2011-04-01 96 views
0

我想讓我的/pages/about成爲只是`/約CakePHP的路由幫助

我試圖在routes.php文件的路由這樣做,但不能把它例如工作Router::connect('/pages/about', array('controller' => 'pages', 'action' => 'display'));

任何人都可以幫忙嗎?

ALSO

爲我的投資組合控制器我有當前顯示的工作像/portfolio/view/102,但我想,以顯示它像/portfolio/view/Paperview_Magazine-102一些地方Paperview雜誌是文章的標題和102是帖子的ID。我看過Cake Book,但如果有人可以發佈一些很棒的代碼。

感謝

回答

1

要製作路線​​,您需要爲​​製作路線,而不是/pages/about

Router::connect('/about', 
       array('controller' => 'pages', 'action' => 'display', 'about')); 

要使用的網址一樣/portfolio/view/Paperview_Magazine-102,你可以使用標準的路線,但你必須做在控制器一點的工作:

// PortfolioController 
// $identifier == "Paperview_Magazine-102" 

public function view($identifier) { 
    if (!preg_match('/^(.+)-(\d+)$/', $identifier, $matches)) { 
     // $identifier is not in format 'Title-Id' 
     $this->cakeError('error404'); 
    } 

    // $matches[1] == Paperview_Magazine 
    // $matches[2] == 102 

    $post = $this->Portfolio->read(null, $matches[2]); 
    $this->set(compact('post')); 
} 
+0

我已經添加代碼到我的控制器,但我仍然需要像'/ portfolio/view/102'這樣的url,是否需要在routes.ph中添加一些內容p文件以及? – Cameron 2011-04-03 11:09:14

+0

@Cameron你當然需要*製作鏈接。 '$ this-> Html-> link(array(...,Inflector :: slug($ post ['title'])。' - '。$ post ['id']))'。這*不*自動發生。 – deceze 2011-04-03 11:36:04

+0

是的,但當訪問實際的網址不起作用?即。 driz.co.uk/cake/portfolio/view/Paperview_Magazine-1 – Cameron 2011-04-03 12:10:11

0

也許有點長鏡頭,但在第一個,你可以嘗試路由器連接::(「/:控制器/約」,陣列(「行動」 =>「顯示」)) ;?我似乎記得我的路線存在一些類似的問題,這可能有所幫助,但我不記得原因。

第二個問題,你不能只是返工視圖函數,因此它接受$ slug參數,而不是$ id,並找到正確的投資組合記錄,而不是搜索slu??或者檢查參數是否爲數字,如果是,則搜索ID;如果不是,則搜索ID。