Laravel Packages

Package Pages

Laravel package middleware

Are you working on your package and want to use middleware? Then this is the tutorial for you. In this article is discussed the creation, exposing and testing of middleware in your laravel packages.

Creating a laravel middleware

Sadly there is no "php artisan make:middleware" in your package environment so you can use them code block below to copy in to your name.

<?php

namespace App\Http\Middleware;

use Closure;

class YourMiddleware
{
    public function handle($request, Closure $next)
    {
        // Perform action

        return $next($request);
    }
}

Making the middleware available from your package

Using the middleware in your own routes

Only using it for a specific route or group

There are multiple ways a

Specific route

Creating your own middleware groups

If you want to make your newly created middleware available as a group, for example when you have multiple middleware that interact with each other than you can add the following code in your package service provider.

 public function boot(Router $router) { 
    $this->loadRoutesFrom(DIR.'/routes.php'); 
    $router->middlewareGroup('admin', [ 
        YOUR_MIDDLEWARE::class, 
    ]);
}

##