4

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.

Tom11
  • 2,419
  • 8
  • 30
  • 56
jovanpreet
  • 103
  • 12

3 Answers3

1

You can adjust the order of routes to solve the problem.

Place add before show , and then laravel will use the first match as route .

Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController@add"]);
Route::get('forum/{ques}', "ForumQuestionsController@show");
KIDJourney
  • 1,200
  • 1
  • 9
  • 22
1

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

Haseena P A
  • 16,858
  • 4
  • 18
  • 35
  • Only first is useful as the {ques} parameter is also a string where closure will not work. But also in first what if I had these two routes in different `Route::group()` – jovanpreet Mar 28 '16 at 09:58
0

I think your {ques} parameter do not get properly. You can try this:

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

If you use any parameters in show method add parameters:

public function show($ques){
}
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
tanvirjahan
  • 164
  • 1
  • 1
  • 8