vendredi 3 juin 2016

Laravel Middleware - how to execute inside a controller method?

I am using multiple views for the same URL, depending if the user is logged in or not.. so mywebsite.com is routed like this:

Route::get('/', 'HomeController@redirector')->name('home');

The controller is this:

public function redirector(){

    if(!\Auth::check()){
        return view('welcome'); 
    }
    else{
        return $this->index();
    }

}

Now, when it runs the index function I need it to run the middleware 'auth', that updates and checks the user. The problem is, I cannot attach it to the route, since they might be unlogged causing a redirection loop. I tried this:

public function redirector(){

    if(!\Auth::check()){
        return view('welcome'); 
    }
    else{
        $this->middleware('auth');
        return $this->index();
    }

}

It does not run the middleware.

If I put it in the costructor method attaching it to index, like this:

 $this->middleware('auth', ['only' => 'index'])

it also won't run.

Any solutions to this?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1X32Eny
via IFTTT

Aucun commentaire:

Enregistrer un commentaire