lundi 28 décembre 2015

Registering Laravel middleware

Just starting to get into laravel and running into major issues migrating my vanilla php to use it.

I created a middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Session;

class QwickAuthCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->session()->has('qwick')) {
            //
            return redirect('home');
        }

        return $next($request);
    }
}

I'm checking to see if a session is set. home is a route I have in routes.php

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

I register it in kernel.php like so;

protected $routeMiddleware = [
    ........
    ........
    'qwickAuth' => \App\Http\Middleware\QwickAuthCheck::class,
];

Now I want to apply the middleware to

Route::get('login', 'WebController@login');

How can I do this?

I have tried;

Route::get('login', ['middleware' => ['qwickAuth'], 'WebController@login');

Laravel has a lot of documentation on their site but for some reason all their code is not giving snippets of how people use this framework. In the middleware documentation all they gave was this;

Route::get('admin/profile', ['middleware' => 'auth', function () {
    //
}]);

How do I know how to use it seeing that I don't have a function in my route



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire