仅在Laravel 5中为一个环境启用全局中间件 [英] Enable global middleware only for one environment in Laravel 5

查看:95
本文介绍了仅在Laravel 5中为一个环境启用全局中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5中使用了全局中间件(barryvdh/laravel-cors),但我只希望它在一个环境(dev)上处于活动状态.那是因为我只在开发环境中需要composer,所以它没有安装在生产环境中.

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.

我注册了它在App Kernel中具有全局中间件,因此如果尝试在生产环境中部署我的应用程序(Class 'Barryvdh\Cors\CorsServiceProvider' not found),则会出现错误.我知道为什么,但是我正在寻找解决方案.

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.

有什么方法可以在laravel 5中全局声明一个中间件,但仅在一个环境中需要吗?

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 :)

推荐答案

到目前为止,我发现的最好方法是检查内核中的env('APP_ENV')

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);
}

这篇关于仅在Laravel 5中为一个环境启用全局中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆