Ad

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('/', '[email protected]')->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', '[email protected]');

How can I do this?

I have tried;

Route::get('login', ['middleware' => ['qwickAuth'], '[email protected]');

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

Ad

Answer

You should use:

Route::get('login', ['middleware' => ['qwickAuth'], 'uses' =>'[email protected]']);

You have example of uses in routing here: https://laravel.com/docs/5.2/routing#named-routes

Ad
source: stackoverflow.com
Ad