且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5,视图::分享

更新时间:2023-11-26 10:28:04

首先,您可以创建自己的BaseController并将其扩展到其他控制器中.

First, you can probably create your own BaseController and extend it in other controllers.

第二件事是,您可以直接在View中使用Auth:user(),而无需在视图中分配任何内容.

Second thing is, that you may use Auth:user() directly in View, you don't need to assign anything in the view.

对于其他用法,您可以转到app/Providers/App/ServiceProvider.php,在boot方法中可以View::share('current_user', 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:

use View;
use Auth;

因为此文件位于App\Providers命名空间中

because this file is in App\Providers namespace