External anchors in CodeIgniter

May 7th, 2010

When using CodeIgniter, I almost never use bare anchor elements to create links.  Instead, I use the anchor(“controller/function”, “text”) function in the url helper.  At first glance, however, it appears that you cannot use the anchor() function to link to external urls and thus have to write the anchor html element yourself.

This is not true. Instead of using the bare html element, I decided that I would extend the helper to include the functionality I wanted.  When I looked into the helper, though, it appears that the CodeIgniter team had already done it!  However, as of CI 1.7.2, the functionality is undocumented. Luckily, it is easy to do.

<?php echo anchor("http://www.codeigniter.com", "CodeIgniter"); ?>

 Instead of specifying a controller and function to link to, supply the anchor() function with a full qualified domain address (http://…).  The helper will write the correct link and you won’t have to do it by hand.

Do you have any other undocumented tips or tricks with the standard CodeIgniter library?  I would love to hear what they are!

2 Responses to “External anchors in CodeIgniter”

  1. Carlos says:

    This is pretty useful I will be sure to remember it for my next project

  2. Cambo says:

    Great! I just had this issue and had some code in place to handle a mixture of internal and external links. I never thought to look at the source. Doh! Anyway I have gotten rid of all that code and the anchor() tag handles both beautifully. I wonder why it’s not documented? Anyway, thanks for revealing this. Really helpful.

Leave a Reply