2010-03-19 30 views
2

設置我的路由的最佳方式是什麼,以便不同類型的帖子具有不同的URL?根據帖子類型路由到不同的URL

例如,正規職位/posts/slug而精選文章是/featured/slug

都鏈接到同一個控制器和行動/posts/view/slug

我嘗試了不同的方法來做到這一點,但收效甚微。目前我鏈接參數看起來像下面這樣:

array('controller' => 'posts', 'action' => 'view', 'featured' ,$post['Post']['slug']) 

編輯:我可以爲每個不同類型的操作,並使用setAction命令使用視圖操作來代替。雖然有更好的方法來做到這一點?

array('controller' => 'posts', 'action' => 'featured', $post['Post']['slug']) 

function featured($slug) { 
    $this->setAction('view', $slug); 
} 

回答

3

我想你也可以通過添加如下行你/app/config/routes.php做到這一點:

Router::connect('/:controller/featured/*',array('action' => 'view')); 

route setting in cookbook

0

這可能會實現:

// 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'] 
)); 
相關問題