Open up app/Http/Kernel.php and find protected $routeMiddleware
Underneath this you’ll see the different shorthands of calling the classes like:
'auth' => \App\Http\Middleware\Authenticate::class,
Which you can find under app/Http/Middleware/Authenticate.php
Now we can create our own middleware with this command:
php artisan make:middleware RoleMiddleware
Open up app/Http/Middleware/RoleMiddleware.php
Registering a new middleware and using it
To put in maintenance mode type:
php artisan down
Then put back up
php artisan up
Now register the RoleMiddleware in Kernel.php.. add to end of this:
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'role' => \App\Http\Middleware\RoleMiddleware::class,
];
Create a route for the middleware that’s basic like:
Route::get('/admin/user/roles', ['middleware'=>'role', function () {
return "Middleware role";
}]);
Now update RoleMiddleware to handle this request to the middleware:
public function handle(Request $request, Closure $next)
{
return redirect('/');
//return $next($request);
}
Now going to the /admin/user/roles URL will simply redirect the user the homepage.
Middleware – roles, migration and relations setup
Create a Role model to store the role of user:
php artisan make:model Role -m
Open up user migration and add this:
$table->integer('role_id');
In new role migration add:
$table->string('name');
Then
php artisan migrate:refresh
Open Role model:
protected $fillable = [
'name',
];
Open User model:
public function role(){
return $this->belongsTo('App\Models\Role');
}
Custom Method
Method 1
Create another middleware:
php artisan make:middleware IsAdmin
Now open up Kernel to add the middleware:
'isAdmin' => \App\Http\Middleware\IsAdmin::class,
Now we’ll need to create roles in our database to use with this case. Add admin and subscriber. Then register a user on the web part at /register.
Create a relationship in User model called isAdmin
public function isAdmin(){
if($this->role->name == 'admin' ){
return true;
}
return false;
}
Now add a route to check whether your current logged in user is administrator (check database to see if it is first!)
Route::get('/', function () {
$user = Auth::user();
if($user->isAdmin()){
echo 'this user is an administrator';
}
//return view('welcome');
});
Now change the user role to see if it works when not an administrator as well.
Method 2
Return homepage route back to original:
Route::get('/', function () {
return view('welcome');
});
Add a redirect instead to the middleware when user role is administrator for IsAdmin:
use Illuminate\Support\Facades\Auth;
public function handle(Request $request, Closure $next)
{
$user = Auth::user();
if(!$user->isAdmin()){
return redirect('/');
}
return $next($request);
}
Next create a route that calls a new Controller:
Route::get('/admin', 'AdminController@index');
Create controller
php artisan make:controller AdminController
Add to AdminController:
public function __construct(){
$this->middleware('isAdmin');
}
public function index(){
return "you are an administrator because you are seeing this page";
}
Now if you are an admin and visit /admin you’ll see the above message. If you aren’t, you’ll be redirected to the homepage.