Laravel 5.5,Auth :: user()在Controller.php中不起作用(Laravel 5.5, Auth::user() not working in Controller.p

编程入门 行业动态 更新时间:2024-10-28 18:26:31
Laravel 5.5,Auth :: user()在Controller.php中不起作用(Laravel 5.5, Auth::user() not working in Controller.php)

我遵循Laravel 5.4中的视频多重身份验证! (管理员+用户) - 第1部分 从part1到5,全部完成。 现在我可以登录,注销。 当我想要获取管理员的属性时购买,我遇到了问题。

在管理员刀片中,我可以使用{{Auth :: user() - > name}},没关系。 在Composer中,它也可以。

<?php namespace App\Http\ViewComposers; use Illuminate\View\View; use Illuminate\Http\Request; use Auth; class AdminComposer { /** * Create a movie composer. * * @return void */ public function __construct(Request $request) { $this->admin= Auth::user(); $this->base = config('app.url') . '/admin/'; } /** * Bind data to the view. * * @param View $view * @return void */ public function compose(View $view) { $view->with('admin', $this->admin); $view->with('base', $this->base); } }

然后我尝试在Controller中使用View,没有任何内容,也没有错误消息。

<?php namespace App\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Auth; class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; public function __construct() { $admin = Auth::user(); echo "<pre>", print_r($admin, 1), "</pre>"; exit; if(!empty($admin)){ View::share('admin', $admin); } } }

为什么Auth :: user()在刀片和作曲程序中没有问题,而不是在控制器中工作?


/config/auth.php

'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], ],

也许我找到了原因。 虽然还没有解决。

在laravel的Controller.php中,它不起作用。 但在我创建的任何其他控制器中,没关系

例如,以下是可以的。 应用程序\ HTTP \控制器\管理\ HomeController.php

public function __construct() { $this->middleware('auth:admin'); parent::__construct(); } public function index() { $admin = Auth::user(); dd(Auth::user()); }

也许这是因为中间件。

在其他文章中 ,有人使用Auth :: guard('mrm') - > User() 但我也是这样做的,我尝试了Auth :: guard('admin') - > User() 依然没有...

I follow the video Multiple Authentication in Laravel 5.4 Natively! (Admins + Users) - Part 1 From part1 to 5, it's all done. Now I can login, logout. Buy when I want to get the admin's properties, I got problem.

In admin blade, I can use {{ Auth::user()->name }}, it's ok. In Composer, it's also ok.

<?php namespace App\Http\ViewComposers; use Illuminate\View\View; use Illuminate\Http\Request; use Auth; class AdminComposer { /** * Create a movie composer. * * @return void */ public function __construct(Request $request) { $this->admin= Auth::user(); $this->base = config('app.url') . '/admin/'; } /** * Bind data to the view. * * @param View $view * @return void */ public function compose(View $view) { $view->with('admin', $this->admin); $view->with('base', $this->base); } }

Then I try to use View in Controller, there is nothing, and no error message.

<?php namespace App\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Auth; class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; public function __construct() { $admin = Auth::user(); echo "<pre>", print_r($admin, 1), "</pre>"; exit; if(!empty($admin)){ View::share('admin', $admin); } } }

Why Auth::user() is ok in blades and composer, not working in controller ?


/config/auth.php

'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], ],

Maybe I found the reason. Though still not resolved.

In laravel's Controller.php, it's not working. But in any other controller I created, it's ok

For example, the following is ok. App\Http\Controllers\Admin\HomeController.php

public function __construct() { $this->middleware('auth:admin'); parent::__construct(); } public function index() { $admin = Auth::user(); dd(Auth::user()); }

Maybe this is because the middleware.

In other post, someone uses Auth::guard('mrm')->User() But I do the same, I tried Auth::guard('admin')->User() Still nothing...

最满意答案

由于Laravel 5.3不再能够访问控制器构造函数中的会话, 因为中间件尚未运行 。 对用户进行身份验证或者获取经过身份验证的用户需要会话中间件已经运行。

您可以在会话中间件运行后定义闭包 (滚动到“构造函数中的会话”)。

Since Laravel 5.3 you are no longer able to access session in controller constructors, because middleware has not run yet. Authenticating a user, or getting the authenticated user requires session middleware to have already run.

You can define a closure (scroll to "Session In The Constructor") that happens after the session middleware has run.

更多推荐

本文发布于:2023-07-28 18:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   Auth   Laravel   user   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!