它是不好的做法,我常常把我的基本控制器内的auth用户如下:Laravel - 控制器构造函数中设置auth用户
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
view()->share('signedIn', Auth::check());
view()->share('user', $this->user);
return $next($request);
});
}
}
或者我应该访问在我的控制方法auth用户如下:
public function index()
{
$user = Auth::user();
}
我基于泰勒提出的意见(https://laravel-news.com/controller-construct-session-changes-in-laravel-5-3)
但是杰弗里方式似乎在构造函数中为数字高程模型要设置这个在Laracast的许多课程中演示过?
[在控制器的构造函数中调用一些中间件]非常普遍(https://laravel.com/docs/5.4/controllers#controller-middleware)。我会说杰弗瑞在那里更快地展示了这个课程,在那里设置了整个中间件代码。去与文档专用中间件,并在构造函数中调用它,你会没事的。 – Tpojka
@Tpojka我知道在控制器的构造函数中调用中间件是很常见的,我的问题与解决在根据泰勒的构造函数中认证的用户是不好的做法有关。 – adam78