Adding custom middleware to protect user admin area
In this section, I am going to use middleware to protect an admin area. We will build this area as our user admin area later, but for now I just want to show how to implement middleware that makes sure that the user is the admin.
Getting ready
Base install of Laravel with users imported.
How to do it…
First, I am going to add a new field to the user table to set some users as admins:
> php artisan make:migration alter_user_table_add_is_admin
I will edit the
database/migrations/2016_05_21_132909_alter_user_table_add_is_admin.php
file so that it looks as follows:Then, I will make middleware to consider this:
> php artisan make:middleware IsAdminMiddleWare
I will then edit the
app/Http/Middleware/IsAdminMiddleWare.php
file, so it looks like this:Then, I will update
app/Http/Kernel.php
to include the newis_admin
middleware:Then, I will make the route for this admin area, which, right now, will not return much; but later on it will...