CodeIgniter Create Post Slugs (CCPS)

I heard SEO experts talking about clean article slugs, I myself don't really understand the definition of "clean" in the discussion. Most of them use wordpress.

But one thing that bothers me is how to generate slugs for my blog articles which are using codeIniter? because I'm sure this is very "unclean" if it's still plain in the form of a default segment uri like this:

www.site.com/index.php/blog/view/7

As far as I know, slugs are permalinks generated from article titles, so that they appear attractive to readers and search engines, perhaps something like this:

www.site.com/index.php/blog/view/once-upon-a-time

The problem is how to make it automatic in codeigniter?

Completion

I just store slugs in my database table, in a column named slugs, then make it a reference / parameter for each article / post. The method is more or less like this:

public function view($slug)
{
    $query = $this->db->get_where('posts', array('slug' => $slug), 1);

    // Fetch the post row, display the post view, etc...
}

Next, we can easily create a slug from the post title, just use  url_title()  which is part of the CodeIgniter URL helper.

// Use dashes to separate words;
// third param is true to change all letters to lowercase
$slug = url_title($title, 'dash', true);

Tips

If you want a unique slug for each article, then just insert a unique key in the slug name, for example, take it from the time maybe, so that your posts are not ambiguous, and can avoid crashes or chaos in your web application.


Post a Comment

Previous Next

نموذج الاتصال