Osijek,
Croatia
+385 95 578 4732
MON - FRI, 4PM - 7PM
ivan@javorovic.com
REPLY IN 48 HOURS
Udemy Course
Ivan Javorović profile image

Ivan Javorović

Full Stack Developer

  • BORN : October 3, 1995
  • NICKNAME : Jawe
  • FAVORITE BAND : The Beatles

Custom middleware in Laravel 5.6 (2018)

2,913 views

Admin, Laravel, Laravel 5.6, php, Kernel, Middleware

In this example we will add a Admin Middleware to our Laravel 5.6 project.


First we need to open up cmd (Win + R and type cmd) and navigate to your project folder and type:


php artisan make:middleware Admin


Now your middleware Admin class is made, you can find it at app/Http/Middleware/Admin.php


Add the following:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->isAdmin())
        {
            return $next($request);
        }

        return redirect('/');
    }

    private function isAdmin(){
        if(Auth::user()->role_id == 1)
            return true;
        else
            return false;
    }
}

The above code will check if the current logged in user has the role_id == 1 (which is Administrator), if not the user will get redirected back to the root page.


Also we need to define the middleware in our $routeMiddleware variable.


You can find it in app/Http/Kernel.php


Add the following to the array:

'admin' => \App\Http\Middleware\Admin::class,



HOW TO USE IT


Using it is very simple, just make a route group in your routes/web.php with the middlewaresauth‘ and ‘admin‘.


Example:

//ADMIN
Route::group(['middleware' => ['auth', 'admin']], function(){
//add code here
});
//END ADMIN

This will check if the user is authenticated and if the user has the role as admin.



OTHER USES FOR MIDDLEWARE


You can use middleware to populate your navbar dropdown for example.


If you have dynamic content which is loaded in your navbar dropdown each time a user visits a page you can make a middleware, add all of the pages that load the navbar in a ‘navbar‘ route group and share the data collected from the database with each page.


Create a new Navbar middleware class:

php artisan make:middleware Navbar


Edit the Navbar class like so:


namespace App\Http\Middleware;
use Closure;
use App;

class Navbar {
  public function handle($request,Closure $next){

   $nav_services = //type your query here

    \View::share('nav_services', $nav_services);

    return $next($request);
  }
}

In the code above I’ve created a variable $nav_services (you have to add your own query this is just an example) and I use \View::share to share this variable with every route which is in my route group carrying the middleware ‘navbar’; which we are going to add right now.


Edit app/Http/Kernel.php


Add the following to $routeMiddleware array:

'navbar' => \App\Http\Middleware\Navbar::class,


And now just use it like so:

//Navbar
Route::group(['middleware' => ['navbar']], function(){
//enter code here
});
//END Navbar

Now in your views you can access the variable with {{$nav_services}}.

By: Ivan Javorović