Welcome Guest, Not a member yet? Register   Sign In
I need some help understanding routes

Hello all,

I have a blog with a url that looks like this:


the /2/ is the blog ID and /6 is the page.

I want the /2/ to be the title of the blog entry. Is this something that is supposed to be done in the routes file or can I do it in my controller? Either way, how is done?


In your routes you might have something like

$route['blog/index/(:any)/(:num)] = 'blog/index/$1/$2';

Then in your controller, you would have something like this...

class Blog extends CI_Controller {
   public function index($title, $page) {
      // Fill in code here to look up the blog entry in your
      // database for the give title and page number
      $this->load->view('blog', array('data' => $data));

I usually just do it in the controller/view.

In controller, you'd read the id and ignore the title, then search your post by the id:

$id = $this->uri->segment(3);
$this->db->where('id', $id);
$posts = $this->db->get('blog_posts');

Then building a link in the view:

echo anchor('controller/function/' . $id . '/' . url_title($title));

That would make a link like:


This works for me, you can still search your post by id, and google gets the title in the url.



If you don't want to have the id at all e.g. http://myblog.com/blog/my-post-title, the way I'd recommend doing it is to save a url_title()'d version of your title as a separate field called url_title or something so you can easily search on it.

Thanks guys.

@bproctor - your solution would be ideal if I could get it to work. I have already removed all the code I used to attempt it. I will try it again later and if I still can't get it to work I will post.

@Joe - Thanks. This solution will work in the mean time.

Theme © iAndrew 2016 - Forum software by © MyBB