Domanda

I would like to have the following url:

http://localhost/question/1/choices/

and optionally /question/1/choices/3

Where 1 is a question_id and 3 is a choice_id.

How can I do that in Laravel?

Thanks

UPDATE I think I could achieve this with something like

Route::get('questions/(:num)/choices/(:num)')

But how can I link it to the controller

Class Questions extends Base_Controller {
    public static $restful = TRUE;
    public function get_choices($question_id, $choice_id) {
        //
    }
}
È stato utile?

Soluzione

Do you'll want a Questions_Controller (remember the _Controller, it's important) like this:

// controllers/questions.php
class Questions_Controller extends Base_Controller {
    public $restful = true;
    public function get_choices($question_id, $choice_id)
    {
        // Use $question_id and $choice_id
    }
}

And your route would look like controller@action, so...

// routes.php
Route::get('questions/(:num)/choices/(:num)', 'questions@choices');

Routing to actions is covered at the end of Controller Routing, just before the section on CLI Route Testing. Wildcards work the same with actions as they do anonymous functions.

Altri suggerimenti

Laravel allows you to use nested resources.. In your case you could have the following in your routes.php

Route::resource('question', 'QuestionController');
Route::resource('question.choices', 'ChoicesController');

Then in your ChoicesController be sure to pass in both question_Id and choice_Id

You can check if everything is routed correctly by running:

php artisan routes
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top