Cakephp路由控制器别名

时间:2014-10-20 13:12:33

标签: php cakephp cakephp-routing

我正在尝试使用此站点执行相同的操作,stackoverflow,使用其URL。

CakePHP的工作方式如下:website / controller / action /

我想配置路由来实现这个目标:

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

例如:myWebSite.com/questions/12874722/cakephp-routing-controller-alias /

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

1 个答案:

答案 0 :(得分:1)

在Config / routes.php中

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

在Controller / QuestionsController.php

查看操作将问题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
}