cooper
cooper

Reputation: 674

Weird behavoiur with route in Laravel

I was modifying a code in order to show different data wether the user is super admin or not and I've found a weird behaviour I never have seen before:

The route file has these routes:

Route::resource('alert_levels','AlertLevelController');
Route::get('alert_levels/{id}','AlertLevelController@index');

And the controller looks like this:

public function index(Request $request){
  $user = Auth::user();
  $company_id = $user->isSuperAdmin() ? $request->company_id : $user->company_id;
  ...
}

The code works fine (only super admin would see a form in which there's a "company_id" input and therefore he/she will send the request parameter), but I've tried to access to the "admin URL" (alert_levels/number) with other user role in order to handle the code (redirect or other), and I've seen that the code looks like it's ingnoring it. It redirects to a blank page, and whatever I do the result is always a blank page. I even have written a dd('hello') on the top of the index controller method but the result is the same.

Please, do you know what I'm doing wrong or why is this the expected behavoiur?

Thanks in advance.

Upvotes: 0

Views: 26

Answers (1)

Alberto
Alberto

Reputation: 12929

You have to swap the routes, otherwise resource will evaluate the response (since it will define the same url structure/method) instead of the one you have defined:

Route::get('alert_levels/{id}','AlertLevelController@index');
Route::resource('alert_levels','AlertLevelController');

Upvotes: 3

Related Questions