2010-11-02 117 views
1

我有以下routes.php文件配置CakePHP的路由器連接::()配置,自定義參數解析

Router::connect('/:type/:slug;:id', array(
     'controller' => 'content', 
     'action' => 'show', 
     'type' => null, 
     'slug' => null, 
     'id' => null, 
    ), 
    array(
     'type' => '(articles|releases|answers|videos)', 
     'slug' => '[a-zA-Z0-9\-]+', 
     'id' => '[0-9]+', 
     'pass' => array('type', 'slug', 'id'), 
    )); 

,我試圖解析以下網址:

/answers/effective-language-therapy-for-people;368 

並且路由器讓我到正確的控制器&動作,但是傾銷$this->params告訴我,它不能正確識別$id$slug

Array 
(
    [type] => answers 
    [slug] => answers 
    [id] => effective-language-therapy-for-people 
    [named] => Array 
     (
     ) 

    [pass] => Array 
     (
      [0] => answers 
      [1] => answers 
      [2] => effective-language-therapy-for-people 
     ) 

    [controller] => content 
    [action] => show 
    [plugin] => 
    [url] => Array 
     (
      [ext] => html 
      [url] => answers/effective-language-therapy-for-people;368 
     ) 

    [form] => Array 
     (
     ) 
) 

那麼 - 什麼給?是我的正則表達式錯誤,方法錯過了什麼,或者是什麼?有任何想法嗎?

注:本人已閱讀:


UPDATE,解決和工作版本

Router::connect('/:type/:slug:splitter:id', array('controller' => 'content', 'action' => 'view',), array(
    'type' => 'articles|releases|answers|videos', 
    'slug' => '[a-zA-Z0-9\-]+', 
    'splitter' => '[\;\-\|]+', 
    'id' => '[0-9]+', 
    )); 
+0

我不喜歡slug和數字之間的分號。 – Leo 2010-11-02 22:14:02

+0

可以理解,對於我所關心的所有人來說,這可能是一個破折號或其他任何東西,但我正在減少此測試可能出現的問題的數量。 – zeroasterisk 2010-11-04 13:04:33

回答

7

嘗試:

Router::connect('/:type/:slug;:id', array(
    'controller' => 'content', 
    'action' => 'show', 
    'type' => null, 
    'slug' => null, 
    'id' => null, 
), 
array(
    'type' => 'articles|releases|answers|videos', 
    'slug' => '[a-zA-Z0-9\-]+', 
    'id' => '[0-9]+', 
    'pass' => array('type', 'slug', 'id'), 
)); 

問題是括號()中的類型,它在CakePHP中不受支持。

+0

就是這樣,將代碼更改爲: \t Router :: connect('/:type /:slug:splitter:id',array('controller'=>'content','action'=>'view' ,),陣列( \t \t '類型'=> '物品|版本|答案|視頻', \t \t '蛞蝓'=> '[A-ZA-Z0-9 \ - ] +', \t \t「 splitter'=>'[\; \ - \ |] +', \t \t'id'=>'[0-9] +', \t \t)); – zeroasterisk 2010-11-04 14:51:57