Last active
December 28, 2015 12:48
-
-
Save abdullahbutt/7502838 to your computer and use it in GitHub Desktop.
anchor in CI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Note: We have to load the 'url' helper first to use anchor function | |
| $this->load->helper('url'); | |
| //echo base_url().'<br>'; | |
| echo anchor('start/hello', 'Say Hello Mate'); | |
| //It is equivalent of: | |
| //<a href="http://localhost/my_project/index.php/start/hello">Say Hello Mate</a> | |
| echo '<br>'; | |
| echo anchor('../../www.w3schools.com', 'Go to W3Schools'); | |
| echo '<br>'; | |
| /* | |
| Remember, there are two advantages to using the CI helper. Firstly, less typing | |
| (44 characters as opposed to 78, both including spaces. If you include loading | |
| the URL helper—another 27 characters, which you only have to do once per controller—it still comes to 76 rather than 82.) | |
| Secondly, the URL helper automatically looks up the site URL in the config files (and the index file name). This means that if you change your site location, you only need to alter the config file once, you don't have to hunt through your code for hyperlinks that don't work any more. | |
| The URL helper has other useful functions. For instance, it can create a | |
| 'mailto' hyperlink. | |
| echo mailto('me@example.com', 'Click Here to Email Me'); | |
| has the same effect as typing this HTML: | |
| <a href="mailto:me@example.com">click here to email me</a> | |
| If you are worried about robots harvesting the email addresses from your website and using them for spamming, change mailto in the CI code to safe_mailto. What appears on your viewer's screen is exactly the same, and works the same way. | |
| However, if you examine the actual HTML code, this has now become a complex heap of JavaScript, which the robot cannot (easily) read: | |
| */ | |
| <script type="text/javascript"> | |
| //<![CDATA[ | |
| var l=new Array(); | |
| l[0]='>';l[1]='a';l[2]='/ | |
| ';l[3]='<';l[4]='|101';l[5]='|109';l[6]='|32';l[7]='|108';l[8]='|105';l[9]='|97';l[10]='|109';l[11]='|101';l[12]='|32';l[13]='|111';l[14]='|116';l[15]='|32';l[16]='|101';l[17]='|114';l[18]='|101';l[19]='|72';l[20]='|32';l[21]='|107';l[22]='|99';l[23]='|105';l[24]='|108';l[25]='|67';l[26]='>';l[27]='"';l[28]='|109';l[29]='|111';l[30]='|99';l[31]='|46';l[32]='|101';l[33]='|108';l[34]='|112';l[35]='|109';l[36]='|97';l[37]='|120';l[38]='|101';l[39]='|64';l[40]='|101';l[41]='|109';l[42]=':';l[43]='o';l[44]='t';l[45]='l';l[46]='i';l[47]='a';l[48]='m';l[49]='"';l[50]='=';l[51]='f';l[52]='e';l[53]='r';l[54]='h';l[55]=' ';l[56]='a';l[57]='<'; | |
| for (var i = l.length-1; i >= 0; i=i-1){ | |
| if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";"); | |
| else document.write(unescape(l[i]));} | |
| //]]> | |
| </script> | |
| You, and your users, need never see this code. It's only there to confuse the robots and keep your email addresses safe from spam. You put it there by adding four letters and an underscore: you wrote safe_mailto instead of mailto, and CI did | |
| the rest. | |
| There are several other useful functions in the URL helper. See the User Guide for more about them and how to use them. | |
| Just consider the URL helper as a whole. Let's go back to the touchstones for coding we discussed earlier in this chapter: | |
| 1) This code has high 'component singularity'. It does a limited range of things, and it's clear what they are. | |
| 2) It is 'loosely coupled'—it has a simple interface and no other dependency on any code that's calling it. You can use the URL helper in any CI project you're writing. Most of your projects will need some sort of hyperlinks, for instance. You can use this helper over and over again to create them. | |
| If you look at the URL helper's code (in system/application/helpers/ | |
| url_helper.php) you'll see that it is procedural code—that is, it is simply a set of functions, not an OO class. It doesn't load any other CI classes or helpers. (Not | |
| being an object, it can't do this.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment