E
E
energimass2018-12-25 15:21:41
Laravel
energimass, 2018-12-25 15:21:41

Why doesn't the method declared in the constructor work?

Why does the gate facade's denies method work like this:

public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){
        if(Gate::denies('VIEW_ADMIN')){
            return redirect('myprofile');
        }
        $array = array(
        'title'=>'Админ панель. Главная.',
        'user'=>$this->getCurUser(),
        );
        if(view()->exists('pages.admin.index.index-content')){
            return view('pages.admin.index.index-content', $array);
        }
        abort(404);
    }

And if you declare it in the constructor (I want the check to pass for all methods), the denies method does not work.
public function __construct()
    {
        $this->middleware('auth');
        if(Gate::denies('VIEW_ADMIN')){
            return redirect('myprofile');
        }
    }

    public function index(){
        $array = array(
        'title'=>'Админ панель. Главная.',
        'user'=>$this->getCurUser(),
        );
        if(view()->exists('pages.admin.index.index-content')){
            return view('pages.admin.index.index-content', $array);
        }
        abort(404);
    }

What should be added or removed? I would be grateful if someone could explain. And somehow I don’t get it myself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kovalchuk, 2018-12-27
@energimass

you need to wrap the code in middleware

public function __construct()
{
    $this->middleware('auth');
    $this->middleware(function ($request, $next) {
        if(Gate::denies('VIEW_ADMIN')){
            return redirect('myprofile');
        }
        return $next($request);
    });
}

Read more https://laravel.com/docs/5.3/upgrade#5.3-session-i...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question