0

I created User Controller policy. In User controller there is a method like this:

public function create(Request $request){

    $this->authorize($request->user());

    return view('frontpage');

}

Policy for this method works pretty fine, except one thing - If is action unauthorized, laravel fires this:

This action is unauthorized.

Instead, I want it to fire 403 error. How to do this?

Ahmed farag mostafa
  • 2,802
  • 2
  • 14
  • 33
Fusion
  • 5,046
  • 5
  • 42
  • 51
  • Try Overriding: `public function forbiddenResponse() { return Response::make(view('errors.403'), 403); }` – Abbasi May 02 '16 at 16:17
  • Ill try, thanks, but where I can find method you mentioned? – Fusion May 02 '16 at 16:21
  • Just ignore my last comment and try this `if($this->authorize($request->user())){ App::abort(403, 'Unauthorized action.');} else { return view('frontpage'); }` if it works for you. – Abbasi May 02 '16 at 16:38
  • The first approach would be a better Idea, because it would make my desired abort(403) as default. The second approach would make my controller much more cumbersome. – Fusion May 02 '16 at 16:45
  • You have to extend your abstract class e.g `Request` from `FormRequest` and in `Request` class override `forbiddenResponse()` – Abbasi May 02 '16 at 16:53
  • I found App\Http\Requests\Request.php - here is Request class extending FormRequest. I wrote in this class following: public function forbiddenResponse(){ dd("boo"); } Sadly, it looks like nothing changed. I am gettin still the same response. – Fusion May 02 '16 at 16:58

0 Answers0