CodeIgniter 4 anchor() URL Helper function

Here as of late, I’m leaning on every piece of built-in functionally of the CodeIgniter 4 framework I can. One I’ve been using a great deal is the anchor() URL helper function. Continue reading to learn more about this function…

Image by b0red from Pixabay

Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


What is the URL anchor() helper function

The anchor() function creates an HTML anchor tag (<a></a>) based on your site URL.

How to use the URL anchor() helper

Unlike many of the other CodeIgniter 4 helper functions, the URL helper is made available by the framework automatically in each request. Therefore, since you do not have to manually load the URL helper, using the anchor() function in any of your views or markup is a matter of just calling the function.

The CodeIgniter 4 anchor() URL helper function syntax is as follows:

anchor([$uri = ''[, $title = ''[, $attributes = ''[, $altConfig = null]]]])
The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.


Suppose we have this HTML markup in one of our views:

<p>This is some text to use the CodeIgniter 4 URL helper anchor function.
<?=anchor(base_url(), 'Click here');?> to find out more about it.</p>

In the below screenshot, we can see the <a></a> element has been created in the paragraph rendered in the browser:

CodeIgniter 4 anchor() function displays <a> element in browser.

If you want to support this blog and my work, please donate to my Tip Jar. Every amount helps and is much appreciated. Thank you.


Checking in the Chrome developer tools Elements tab, we can see the raw HTML code:

Notice the base_url() function is essentially the href attribute while the 2nd ‘Click here‘ parameter is the text that is located between the opening and closing HTML anchor tags.

📝 Note: The base_url() function is also part of the URL helper library and returns the site-based URL that is specified in the site config. This function also can accept a parameter if desired.

URL helper anchor() function: attributes

The CodeIgniter 4 anchor() URL helper function accepts an optional 3rd parameter of attributes – as an associative array or a string – according to what your needs are. The following examples cover both variations with the PHP code and resulting raw HTML as viewed in the Chrome Developer console.

Attributes as an associative array
<?=anchor(base_url(), 'Click here', ['id' => 'my_link', 'name' => 'my_link', 'rel' => 'nofollow', 'target' => '_blank']);?>
Attributes as a string
<?=anchor(base_url(), 'Click here', 'id="my_link" name="my_link" rel="nofollow" target="_blank"');?>

As you can see, in both instances, the <a></a> element has all of the specified attributes.

CodeIgniter 4 Similar Reading

I’ve written several blog posts about CodeIgniter 4. Feel free to visit any of the below posts and share them with others. Thanks!

As always, if you have any questions or see any mistakes in the code, please let me know via the comments. Constructive comments help me provide accurate blog posts and are much appreciated. Thank you for reading!

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Thank you for reading this post. Please share it with someone else who would enjoy it as well.


Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.

Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Promotional: I am making Gmail HTML Email Signature Templates over in my Etsy shop. Make your emails stand out and pop with your very own.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.