2013-02-26 42 views
1

首先,我明白有很多問題和解決方案,但沒有找到解決我的問題。CakePHP反向路由:Html鏈接沒有顯示正確的鏈接

綜上所述,HTML幫助是無法扭轉正確,而非網址的路由是這樣的:

http://website.com/user_of_website/slug_name 

我得到的網址是這樣的:

http://website.com/things/view/username:user_of_website/slug:a_thing 

這裏是我的路由器設置:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); 
/** 
* ...and connect the rest of 'Pages' controller's urls. 
*/ 
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); 

//User routes 
Router::connect('/login', array('controller' => 'users', 'action' => 'login')); 
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout')); 
Router::connect('/register', array('controller' => 'users', 'action' => 'add')); 

Router::connect('/users/:action/*', array('controller' => 'users')); 
Router::connect('/things/:action/*', array('controller' => 'things')); 

Router::connect(
    '/:username/:slug', 
    array(
     'controller' => 'things', 
     'action' => 'view' 
    ),array(
     'username' => '[a-zA-Z0-9_]+', 
     'slug' => '[a-zA-Z0-9_]+', 
     'pass'=>array(
      'username', 
      'slug' 
     ) 
    ) 
); 
Router::connect('/:userName', array('controller' => 'things', 'action' => 'user'),array('pass'=>array('userName'))); 

而我的Html幫手:

echo $this->Html->link('View', array(
    'controller' => 'things', 
    'action' => 'view', 
    'username' => 'user_of_website', 
    'slug' => 'a_thing' 
),array(
    'class' => 'text-warning' 
)); 

我真的不明白我做錯了什麼。任何幫助不勝感激。

NB,我使用CakePHP 2.3

+0

不知道這會影響你的問題,但最後路線('/:userName')用途一個不同的符號(camelcased)*和*不包含用戶名參數 – thaJeztah 2013-02-26 22:35:27

+0

@thaJeztah的定義/正則表達式,是的我正在測試一些東西,但不相關。這個問題是當我測試/用戶名/ slu links鏈接 – ST0N3 2013-02-27 13:27:23

回答

1

您是否嘗試過,如果它,如果你禁用things「正規軍」路線的作品?例如。 /things/:action/*

我的猜測是,由於通配符的緣故,這條路線也會與網址匹配,而且,因爲它定義了之前的您的自定義路線,將會替代匹配。

如果能解決問題,您可以嘗試下面移動你的「自定義」路由路線

+0

謝謝你,這是行不通的。然而任何鏈接到/ things/action被/:username /:slug規則所捕獲。我通過添加僅影響該操作的規則來解決此問題,例如/ things/add 這很好,因爲只有幾個動作。我必須確保我設置了一個健壯的用戶名黑名單。再次感謝。 – ST0N3 2013-02-27 13:49:39

+0

很高興我能幫到你。您也可以修改「用戶名」正則表達式,以便排除控制器名稱。 OR(可能最好?)將自定義路由*放置在默認的CakePHP路由下面。這樣CakePHP路由將在使用您的路由之前匹配 – thaJeztah 2013-02-27 17:34:15