使变量可以访问整个Laravel 5.2应用程序(Make variable accesible to whole Laravel 5.2 application)

我目前正在使用Laravel 5.2开发Web应用程序,在数据库中我保存了一些配置,我在应用程序中使用了很多控制器的设置,所以我不想一遍又一遍地咨询同样的事情。 我只想在应用程序启动时查阅它,这样也可以减少代码而不会重复。 我读过这个,但似乎是对旧版本框架的旧答案。 那么,在Laravel 5.2中我该怎么办?

I'm currently developing a Web Application using Laravel 5.2, in the database I have some configurations saved, I use that Settings along a lot of controllers in the application, so I don't want to be consulting over and over again the same thing. I want to consult it only on application boot, and it this way also reduce the code and no be repetitive. I read this but it seems like are old answers to old versions of the framework. So, In Laravel 5.2 how can I do it?

最满意答案

在您问题的给定链接中,为Laravel 4.x提供了答案,如果您想在5.2及更高版本中实现相同的技术,那么您可以使用全局中间件。 要做到这一点,只需使用以下内容从终端创建中间件:

php artisan make:middleware GlobalConfigMiddleware

这将在您的app/Http/Middleware目录中创建一个中间件( GlobalConfigMiddleware.php )。 在这个文件中,你需要实现在handle方法中设置配置的逻辑,它应该类似于以下内容:

public function handle($request, Closure $next) { // Pseudo Code app()->singleton('site_settings', function($app) { // Imagine you have the Setting Eloquent Model // Get all settings using the Setting model return \App\Setting::all(); }); // If you use this line of code then it'll be available in any view // as $site_settings but you may also use app('site_settings') as well view()->share('site_settings', app('site_settings')); return $next($request); }

然后,在app/Http/Kernel.php类的$middleware属性中添加此中间件类,例如:

protected $middleware = [ // ... \App\Http\Middleware\GlobalConfigMiddleware::class, ];

现在,您可以像这样使用配置:

$site_settings = app('site_settings');

了解中间件和绑定 。 还要记住,这可以通过不同的方式完成。

In the given link in your question, there the answers were given for Laravel 4.x and if you want to implement the same technique in version 5.2 and later then you can use a global middleware. To do that, just create a middleware from your terminal using something like the following:

php artisan make:middleware GlobalConfigMiddleware

This'll create a middleware (GlobalConfigMiddleware.php) in your app/Http/Middleware directory. In this file, all you need to implement the logic for setting up the config within the handle method which should look something like the following:

public function handle($request, Closure $next) { // Pseudo Code app()->singleton('site_settings', function($app) { // Imagine you have the Setting Eloquent Model // Get all settings using the Setting model return \App\Setting::all(); }); // If you use this line of code then it'll be available in any view // as $site_settings but you may also use app('site_settings') as well view()->share('site_settings', app('site_settings')); return $next($request); }

Then, add this middleware class in the $middleware property of your app/Http/Kernel.php class, for example:

protected $middleware = [ // ... \App\Http\Middleware\GlobalConfigMiddleware::class, ];

Now, you can use the config like this:

$site_settings = app('site_settings');

Learn about Middleware and Binding. Also remember that, this could be done in different ways.

更多推荐