Reputation: 2003
What would be the best way to set up my routes so different types of posts have different URLs?
For example, regular posts are /posts/slug
while featured posts are /featured/slug
Both link to the same controller and action /posts/view/slug
.
I experimented with different ways of doing this but with little success. Currently my link params look something like the following:
array('controller' => 'posts', 'action' => 'view', 'featured' ,$post['Post']['slug'])
Edit: I could create an action for each different type and use setAction to use the view action instead. Although is there a better way to do this?
array('controller' => 'posts', 'action' => 'featured', $post['Post']['slug'])
function featured($slug) {
$this->setAction('view', $slug);
}
Upvotes: 2
Views: 153
Reputation: 4203
This might work:
// app/config/routes.php
Router::connect('/featured/:slug', array(
'controller' => 'posts',
'action' => 'view',
'type' => 'featured'
));
Router::connect('/posts/:slug', array(
'controller' => 'posts',
'action' => 'view',
'type' => 'normal'
));
// one of your views
echo $html->link('Featured post title', array(
'controller' => 'posts',
'action' => 'view',
'type' => 'featured',
'slug' => $post['Post']['slug']
));
or
echo $html->link('Normal post title', array(
'controller' => 'posts',
'action' => 'view',
'type' => 'normal',
'slug' => $post['Post']['slug']
));
Upvotes: 0
Reputation: 8356
I think you can also do this by adding follow line in your /app/config/routes.php:
Router::connect('/:controller/featured/*',array('action' => 'view'));
Upvotes: 3