2014-10-20 148 views
1

我正在尝试做与本网站,stackoverflow一样的网址。Cakephp路由控制器别名

CakePHP的工作原理是这样的:网站/控制器/动作/

我想配置路由来实现这一点:

myWebSite.com/questions/(question_id)/ (问题)/

例如:myWebSite.com/questions/12874722/ cakephp的路由控制器混叠/

我没有想出如何做到这个URL的粗体部分。

+0

这是什么都与“_controller aliases_做“?大胆的部分是重头戏的问题标题。究竟是什么实际问题?你读过[**文档**](http://book.cakephp.org/2.0/en/development/routing.html#passing-parameters-to-action)吗? ps,请始终提及您的确切CakePHP版本并相应地标记您的问题。 – ndm 2014-10-20 13:18:33

+2

请注意,SO实际上忽略了问题标题,并且可以在其中放置[任何你喜欢的](http://stackoverflow.com/questions/26466567/this-is-a-silly-url),但是如果你得到它错误(这是问题ID是重要的) – DavidG 2014-10-20 13:20:30

+0

实际的问题是,我不知道如何把它放在那里,URL中的问题标题。 – user1148875 2014-10-20 13:23:48

回答

1

在你的config/routes.php文件

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

在控制器/ QuestionsController.php

视图操作GET问题id作为

public function view() { 
    $question_id = isset($this->request->params['pass'][0]) ? $this->request->params['pass'][0] : ""; 
    $question = isset($this->request->params['pass'][1]) ? $this->request->params['pass'][1] : ""; 

    if(empty($question_id)) { 
     throw new NotFoundException('Could not find that question'); 
    } 

    // if $question is empty then get slug from database and redirect to /question_id/question 


    // Get question details and set 
}