Welcome Guest, Not a member yet? Register   Sign In
url_title
#1

Hi,
reading the documentation I see the url_title() function can create human-friendly URL. Using the function I can get whatever string I want but how to display it on the URL?
Reply
#2

(This post was last modified: 12-15-2020, 11:52 AM by seunex.)

Url_title is also know has slug you can use this tag to turn a blog title to slugable url and save it in your database then use the slug to fetch the post. That is when you will need to send the slug to the URL.
For example https'//mysite.com/my-post-title

Happy coding
Reply
#3

Use:

PHP Code:
mb_url_title($str[, $separator '-'[, $lowercase FALSE]]);

This function works the same as url_title() but it converts all accented characters automatically
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

Hi, probably I did not explain the problem. Here is the situation.

When I click on a certain link I call a controller that handles a product page. I want the URL to display the title of that specific product.
Since i have thousands of different products I cannot use routing. I was hoping that url_title could dynamically change the URL based on the page title. But maybe I'm wrong. If so, how to go about it? Thanks
Reply
#5

Then what you need to do is add a field to your table of products called slug varchar 255 you use url_title to create the slug

this way you can display it into the url.

Here is a helper I have for making the slugs.

PHP Code:
**
 * -----------------------------------------------------------------------
 *
 * 
Defaults to $separator '-' can use '_' underscore
 
$lowercase defaults to false.
 *
 * 
Usage:
 *
 * Use 
the following code to slugify a string:
 *
 * 
$slug makeSlug($string$separator$lowercase);
 */

// Check if the function does not exists
if ( ! function_exists('makeSlug'))
{
    
/**
     * slugify ()
     * -------------------------------------------------------------------
     *
     * @param  string $string
     * @param  string $separator
     * @param  bool   $lowercase
     * @return string
     */
    
function makeSlug(string $stringstring $separator '-'bool $lowercase false) : string
    
{
        
helper('url');

        
// make the Slug and return the string, auto loads the text helper
        
return mb_url_title($string$separator$lowercase);
    }
}

// ----------------------------------------------------------------------- 

You will need to create a route for your products to use the slug.

You can see how to do the routing here on @Includebeer's website.

How to build a basic web application with CodeIgniter 4 (part 3)

If you look at his url for this page you will see what can be done.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(12-17-2020, 07:53 AM)InsiteFX Wrote: Then what you need to do is add a field to your table of products called slug varchar 255 you use url_title to create the slug

this way you can display it into the url.

Here is a helper I have for making the slugs.

PHP Code:
**
 * -----------------------------------------------------------------------
 *
 * 
Defaults to $separator '-' can use '_' underscore
 
$lowercase defaults to false.
 *
 * 
Usage:
 *
 * Use 
the following code to slugify a string:
 *
 * 
$slug makeSlug($string$separator$lowercase);
 */

// Check if the function does not exists
if ( ! function_exists('makeSlug'))
{
    
/**
     * slugify ()
     * -------------------------------------------------------------------
     *
     * @param  string $string
     * @param  string $separator
     * @param  bool   $lowercase
     * @return string
     */
    
function makeSlug(string $stringstring $separator '-'bool $lowercase false) : string
    
{
        
helper('url');

        
// make the Slug and return the string, auto loads the text helper
        
return mb_url_title($string$separator$lowercase);
    }
}

// ----------------------------------------------------------------------- 

You will need to create a route for your products to use the slug.

You can see how to do the routing here on @Includebeer's website.

How to build a basic web application with CodeIgniter 4 (part 3)

If you look at his url for this page you will see what can be done.

Ok, thanks a lot! I'll give it a try
Reply




Theme © iAndrew 2016 - Forum software by © MyBB