Creating URL-safe slugs in CodeIgniter 4
Created:27 Jan 2022 12:25:32 , in Web development
Today it is very desirable to be able to create user (and search engine) friendly URLs. One big step towards achieving this is knowing how to build slugs. A slugs is a special string, which is suitable for including in an URL. For example a slug has no spaces in it. They all are replaced by a character like an underscore or a dash. Also, slugs have no accented characters. They have them converted to their closest ASCII characters.
In this short post I want to give a quick overview on how to create slugs using functions in CodeIgniter 4.
CodeIgniter 4 has many useful helpers. Two of them which are particularly interesting in the context of creating slugs are URL and Text helpers. Here is why.
URL helper has function called url_title. It allows you to build slugs with just one line of code.
If a variable called $tagName points to a string to be converted to a slug, just do this:
You will get a string with all spaces replaced with the dash character and upper case character converted to their lower case ASCII counterparts. This is enough for strings made up of ASCII characters, or in short, stuff written in English language.
However, If your input string contains accented characters, url_title will not convert them. You need a function called convert_accented_characters too. The function is a part of Text Helper. So before using it, make sure you include the helper in your controller method or view (Text Helper, unlike URL Helper, is not loaded automatically by CodeIgniter 4 applications).
Here is a better version of a slug, one with all accented characters converted to their ASCII counterparts:
The result can be included in a URL. But wait! There is a cherry on the cake.
As it turns out, there exists a function in URL Helper, which can do all the above in one go for you.
It is called mb_url_title. The function accepts the same arguments as url_title but also converts accented characters the way convert_accented_characters() does. Since the function is a part of URL Helper there is no need for including anything before it can be called. As said earlier, the framework does it for you automagically. Can't get any better, can it?
That's pretty much on building slugs in CodeIgniter 4 from me. Have fun while building your own and if you have something to add on the subject, leave a comment below!
This post was updated on 27 Jan 2022 12:28:03
Author, Copyright and citation
Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.
©Copyright, 2023 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.
Computer code, if present in the article, is excluded from the above and licensed under GPLv3.
Cite this article as:
Wojnowski, Sylwester. "Creating URL-safe slugs in CodeIgniter 4." From sWWW - Code For The Web . https://swww.com.pl//main/index/creating-url-safe-slugs-in-codeigniter-4