Enable global middleware only for one environment in Laravel 5

血红的双手。 提交于 2019-12-23 07:07:44

问题


I'm using a global middleware in Laravel 5 (barryvdh/laravel-cors) but I only want it to be active on one environnement (dev). That's because I only require it with composer in dev environnement, so it's not installed in production.

I registered it has a global middleware in App Kernel and so I have an error if I try to deploy my app in production (Class 'Barryvdh\Cors\CorsServiceProvider' not found). I know why, but I'm looking for a solution.

Is there any way to declare a middleware globally in laravel 5 but only required in one environnement ?

I hope it's clear enough, I can edit my post if not :)


回答1:


The best way I've found so far is to check env('APP_ENV') in the Kernel

public function __construct(Application $app, Router $router)
{
    if (env('APP_ENV', 'production') === 'local') {
        $this->prependMiddleware('Clockwork\Support\Laravel\ClockworkMiddleware');
    }

    parent::__construct($app, $router);
}



回答2:


To achieve this simply load kernel in the service provider then call either pushMiddleware or prependMiddleware

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Kernel $kernel)
    {
        if ($this->app->environment() === 'dev') {
            $kernel->prependMiddleware(ClockworkMiddleware::class);
        }
    }
}



回答3:


In the kernel.php file

 //...
 protected middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'App\Http\Middleware\VerifyCsrfToken',
    (!$this->app->environment('local')) ?: 'Path/To/Package'
 ]

This should solve.




回答4:


I had the opposite query to yours - wanting to implement middleware globally in production but not in local development. I tried the __construct() method EspadaV8 recommended but it did work for me as env('APP_ENV') failed to return a value that I could test.

What I did instead was to include my middleware in the Kernel $middleware array and then, in the handle($request, Closure $next) method in my middleware, I bracketed all of my code - except return $next($request); - with the following:

if(env('APP_ENV') !== 'local') { 

.......middleware code.......

}

Perhaps the opposite would work for you (i.e. === instead of !==)?

I suspect this is not the best solution - as you point out, it would be ideal to handle the loading of the middleware (or not) in the Kernel class but in lieu of finding something better as of yet, I thought to put this solution out there. Hope it helps!



来源:https://stackoverflow.com/questions/28452814/enable-global-middleware-only-for-one-environment-in-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!