filter a string to remove disallowed characters to compose an URL in CodeIgniter – Education Career Blog

I’m trying to make URL-friendly links for the blog on my portfolio.
So I would like to obtain links something like site/journal/post/{title}

Obviously Journal is my controller, but let’s say my title would be ‘mysite.com goes live!’ I would like to have a valid url like site/journal/post/mysitecom-goes-live where all disallowed characters are removed.

How would I transform ‘mysite.com goes live!’ to ‘site/journal/post/mysitecom-goes-live’ in CodeIgniter based on the characters in $config’permitted_uri_chars’

,

use the url helper

$this->load->helper('url');

$blog_slug = url_title('Mysite.com Goes live!');

echo $blog_slug //mysitecom-site-goes-live 
// might differ slightly, but it'll do what you want.

to generate url-friendly links.

Store this value in a field in your blog table (url_title/url_slug) whatever.

make a function:

class Journal extends controller
{
   //make your index/constructor etc

   function view($post)
   {
     $this->blog_model->get_post($post);
     // etc - your model returns the correct post,
     // then process that data and pass it to your view
   }
}

your blog_model has a method get_post that uses CI’s

$this->db->where('url_title', $post);

hope that makes sense.

then when you access the page:

site.com/journal/view/mysite-goes-live

the function will pick up “mysite-goes-live” and pass it to the view() function, which in turn looks up the appropriate blog entry in the database.

Leave a Comment