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


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.

Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.

Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. 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.