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

[eluser]matches[/eluser]
Hello all,

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

/blog/index/2/6

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?

Thanks,
#2

[eluser]bproctor[/eluser]
In your routes you might have something like

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

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

Code:
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));
   }
}
#3

[eluser]SitesByJoe[/eluser]
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:

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

Then building a link in the view:

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

That would make a link like:

http://mysite.com/controller/function/2/...page-title

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

Hurray!

Also~

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.
#4

[eluser]matches[/eluser]
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