Reputation: 25
I've been searching around to figure out how to rewrite URLs in cakephp, and I thought I had it figured out, but I'm not sure how to rewrite a url in this case.
I currently have - www.example.com/posts/view/1
and I want to replace the ending id of a post with the name of the post.
I want to have - www.example.com/posts/view/$posts['Post']['title']
I thought I could do this:
Router::connect('/posts/view/:title', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('title')));
Turns out I can't, how can I rewrite this url? I've also read about using a slug? is this a case when I would use it?
Upvotes: 1
Views: 813
Reputation: 342
first create unique slug for every post then slug replace with title in browser .after replacing title your Routeing as
Router::connect('/posts/view/:slug', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('slug')));
because we need unique identifier for every post if we are not using id.
Upvotes: 1