简体   繁体   English

Cakephp路由控制器别名

[英]Cakephp routing controller alias

I'm trying to do the same as this site, stackoverflow, do with their URLs. 我正在尝试做与该网站相同的工作,即stackoverflow,使用其URL。

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

I want to config routing to achieve this: 我想配置路由来实现这一点:

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

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

I didnt figured it out how to do this bold part of URL. 我没有弄清楚如何执行URL的这一粗体部分。

In your Config/routes.php 在您的Config / routes.php中

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

In Controller/QuestionsController.php 在Controller / QuestionsController.php中

view action get question id as 查看动作获取问题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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM