1
votes

I'm trying to do the same as this site, stackoverflow, do with their URLs.

CakePHP works like this: website/controller/action/

I want to config routing to achieve this:

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

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

I didnt figured it out how to do this bold part of URL.

1
What does this have to do with "controller aliases"? The bold part is the slugged question title. And what exactly is the actual problem? Have you read the docs? ps, please always mention your exact CakePHP version and tag your question accordingly. - ndm
Notice that SO actually ignores the question title and you can put whatever you like in there, but it will redirect if you get it wrong (it's the question ID that is important) - DavidG
The actual problem is that i don't know how to put it there, the question title in URL. - user1148875
There are sluggable behaviour check them, also save the slug to your database to redirect user to proper url just like S.O. as mentioned by @DavidG - Abhishek
@DavidG A common pattern among websites. Redirect on (SLUG != URI_SEGMENT[N]) - Ryan

1 Answers

1
votes

In your Config/routes.php

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

In Controller/QuestionsController.php

view action get question id as

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
}