php Laravel 5,查看::分享
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26275820/
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, View::Share
提问by Kiwi
I'm trying to do a view::share('current_user', Auth::User());
but in laravel 5 i can't find where to do this, in L4 you could do this in the baseController, but that one doesn't exists anymore.
我正在尝试做一个,view::share('current_user', Auth::User());
但在 laravel 5 中我找不到在哪里执行此操作,在 L4 中您可以在 baseController 中执行此操作,但那个已经不存在了。
grt Glenn
格伦
回答by xwlee
I am using Laravel 5.0.28, view::share('current_user', Auth::User())
doesn't work anymore because this issue https://github.com/laravel/framework/issues/6130
我正在使用 Laravel 5.0.28,view::share('current_user', Auth::User())
不再工作,因为这个问题https://github.com/laravel/framework/issues/6130
What I do instead is, first create a new service provider using artisan.
我要做的是,首先使用 artisan 创建一个新的服务提供者。
php artisan make:provider ComposerServiceProvider
Then add ComposerServiceProvider to config/app.php
providers array
然后将 ComposerServiceProvider 添加到config/app.php
providers 数组
//...
'providers' => [
//...
'App\Providers\ComposerServiceProvider',
]
//...
Then open app/Providers/ComposerServiceProvider.php
that just created, inside boot method add the following
然后打开app/Providers/ComposerServiceProvider.php
刚刚创建的,在boot方法里面添加以下内容
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
View::composer('*', function($view)
{
$view->with('current_user', Auth::user());
});
}
Finally, import View
and Auth
facade
最后,导入View
和Auth
门面
use Auth, View;
For more information, see http://laravel.com/docs/5.0/views#view-composers
回答by Marcin Nabia?ek
First, you can probably create your own BaseController and extend it in other controllers.
首先,您可以创建自己的 BaseController 并将其扩展到其他控制器中。
Second thing is, that you may use Auth:user()
directly in View, you don't need to assign anything in the view.
第二件事是,您可以Auth:user()
直接在视图中使用,您不需要在视图中分配任何内容。
For other usages you can go to app/Providers/App/ServiceProvider.php
and in boot
method you can View::share('current_user', Auth::User());
but or course you need to add importing namespaces first:
对于其他用法,您可以转到app/Providers/App/ServiceProvider.php
并在boot
方法中,View::share('current_user', Auth::User());
但是或当然您需要先添加导入命名空间:
use View;
use Auth;
because this file is in App\Providers
namespace
因为这个文件在App\Providers
命名空间中
回答by Kiwi
This will may help:
这将有助于:
App::booted(function()
{
View::share('current_user', Auth::user());
});
回答by Deivide
In Laravel 5 uses the same method as in laravel 4:
在 Laravel 5 中使用与在 Laravel 4 中相同的方法:
View::share('current_user', Auth::User());
or using the view helper:
或使用视图助手:
view()->share('current_user', Auth::User());
回答by Tang Si
I'v tried it, put it in app/Providers simply not working. The alternative way is to create a Global middleware and put View::share('currentUser', Auth::user()); there.
我试过了,把它放在 app/Providers 中根本不起作用。另一种方法是创建一个全局中间件并放置 View::share('currentUser', Auth::user()); 那里。