Categories
Laravel Samuel

REgistering Middleware

After floundering with creating custom middleware today I wanted to type up my notes for future use. I apologize about the lack of polish these are for my own personal user later:

1.) Create new middleware file: php artisan make:middleware FileName

2.) Add whatever rules are needed to this file (note see SFPS or Fire for great examples)

3.) Register your middleware. Open up app/Http/Kernel.php and find the following section:

protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

*Just toss your new middleware in the mix, so an example I just added is:
'auth_administrator' => \App\Http\Middleware\AuthAdminister::class,

4.) Add your route group to web.php file, something like:

Route::group(['middleware' => 'auth_administrator'], function () {

// include all the routes you want to protect

});

5.) If you need to redirect a customer to their specific page then create this new page view and route, something like:
Route::get('/customer-dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@customer']);