jovanpreet jovanpreet - 5 months ago 88
PHP Question

Laravel 5 single route multiple controller method

I have a route with parameter

Route::get('forum/{ques}', "ForumQuestionsController@show");


Now I want a route something like

Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController@add"]);


well when I hit
localhost:800/forum/add
I get routed to
ForumQuestionsController@show
instead of
ForumQuestionsController@add


Well I know I can handle this in show method of
ForumQuestionsController
and return a different view based on the paramter. But I want it in this way.

Answer

First give this one

Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController@add"]);

Then the following

Route::get('forum/{ques}', "ForumQuestionsController@show");

Another Method (using Regular Expression Constraints)

Route::pattern('ques', '[0-9]+');
Route::get('forum/{ques}', "ForumQuestionsController@show");

If ques is a number it will automatically go to the show method, otherwise add method