Laravel 5 - 在服务提供者中共享变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28356193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 - Share variables in Service Providers
提问by Lloople
I need to load something from database at the beggining of the request and can use this in every place of my application, just one query and full access.
我需要在请求开始时从数据库加载一些东西,并且可以在我的应用程序的每个地方使用它,只需一个查询和完全访问。
Someone told me that I can do that inside ServiceProvider, but I don't know how, and reading the documentation didn't help me.
有人告诉我,我可以在 ServiceProvider 中做到这一点,但我不知道如何做到,阅读文档对我没有帮助。
Can someone show me an example of how to share the Config::all()
to gain access in all controllers and all views?
有人可以向我展示如何Config::all()
在所有控制器和所有视图中共享以获取访问权限的示例吗?
回答by manix
I recommend to you make a Middleware class. In middleware folder create a new class, lets say GlobalConfig
. Each middleware class has a handle()
method that receives a incoming request in order to modify it, or at your case do a specific task before it gone complete . At this point, you can share the data you want:
我建议你制作一个中间件类。在中间件文件夹中创建一个新类,比如说GlobalConfig
. 每个中间件类都有一个handle()
方法,用于接收传入请求以对其进行修改,或者在您的情况下在完成之前执行特定任务。此时,您可以共享您想要的数据:
<?php
namespace MyApp\Http\Middleware;
use Closure;
class GlobalConfig {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// asignning data
view()->share('config', [1, 2, 3]);
// let the request follows its flow
return $next($request);
}
}
}
Well, as I said the middleware can handlean incoming request, but we need to specify when to handle it. There are two specific points when you can capture it:
好吧,正如我所说,中间件可以处理传入的请求,但我们需要指定何时处理它。当您可以捕获它时,有两个特定点:
1. On every request
1. 每一个请求
If you want to play with it on every incoming request, the you need to attach the middleware class into the $middleware
array at app/HTTP/Kernel.php
file like so:
如果您想在每个传入请求上使用它,您需要将中间件类附加到文件中的$middleware
数组中,app/HTTP/Kernel.php
如下所示:
protected $middleware = [
'Illuminate\View\Middleware\ShareErrorsFromSession',
'MyApp\Http\Middleware\GlobalConfig'
];
2. For a specific route
2.对于特定路线
If you want to apply the middleware class just for a certain route or routes you need to set an alias to your middleware class in the $routeMiddleware
array at app/HTTP/Kernel.php
file like so:
如果您只想为某个或多个路由应用中间件类,则需要$routeMiddleware
在app/HTTP/Kernel.php
文件中的数组中为中间件类设置别名,如下所示:
protected $routeMiddleware = [
'globalConfig' => 'MyApp\Http\Middleware\GlobalConfig',
];
Finally, just add the alias each route you want:
最后,只需添加您想要的每个路由的别名:
Route::get('profile', ['middleware' => 'globalConfig', 'uses' => 'ProfileController@show']);
回答by user2094178
Regarding app/Providers/AppServiceProvider.php
, if you place the following code within its boot method:
关于app/Providers/AppServiceProvider.php
,如果您将以下代码放在其引导方法中:
Config::set(['user' => ['name' => 'John']]);
Then anywhere in your app you can grab that value with Config::get('user.name')
;
然后在您的应用程序中的任何地方,您都可以使用Config::get('user.name')
;
So you can feed a config with a result from an Eloquent model, just convert the result to an array.
因此,您可以将 Eloquent 模型的结果提供给配置,只需将结果转换为数组即可。
Then of course you can create a ServiceProvider
to deal exclusively with this.
那么当然你可以创建一个ServiceProvider
专门处理这个问题。
回答by FurkanO
Laravel 4 Solution
In Laravel 4 , you might handle this by simply doing this.
In your BaseController ,
Create something like this,
Laravel 4 解决方案
在 Laravel 4 中,您可以通过简单地执行此操作来解决此问题。
在你的 BaseController 中,
创建这样的东西,
<?php
class BaseController extends Controller {
public function __construct () {
$this->beforeFilter(function() {
View::share('config', Config::all());
});
}
}
Then you should do this in the controllers you want to share the data,
然后你应该在要共享数据的控制器中执行此操作,
class ControllerYouWantToUseData extends BaseController {
public function __construct() {
parent::__construct();
}
}
That is all to share the data in your views. You can use the variable in your views by just calling the $config variable.
这就是在您的视图中共享数据的全部内容。您可以通过调用 $config 变量在视图中使用该变量。