gpt4 book ai didi

laravel - Auth::guard ('user' )->user() 在 Laravel 5.3 中为空

转载 作者:行者123 更新时间:2023-12-03 21:43:51 24 4
gpt4 key购买 nike

我正在尝试将当前用户对象传递给所有模板,如下所示:

class Controller extends BaseController
{
public function __construct()
{
view()->share('usr', Auth::guard('user'));
}
}

每个 Controller 都由 Controller 扩展。但如果我尝试转储 Auth::guard('user')->user() Laravel 返回空值,尽管我已登录。此外,当我将此变量传递给模板时, {{ $usr->user() }}返回当前用户。我做错了什么?

我的配置/auth.php
'defaults' => [
'guard' => 'user',
'passwords' => 'users',
],
'guards' => [
'user' => [
'driver' => 'session',
'provider' => 'user',
],
],
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],

内核.php
         protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
//\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
'throttle:60,1',
'bindings',
],
];

/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

我自己的登录功能:
public function authorizes(Request $request)
{
$this->validate($request, [
'login' => 'required|max:50',
'password' => 'required|max:50'
]);

$credentials = $request->only(['login', 'password' ]);
$remember = $request->get('remember', false) == 1 ? true : false;

if ($this->guard->attempt( $credentials, $remember)) {
$user = $this->guard->user();
$user->last_login = date('Y-m-d H:i:s');
$user->save();
return redirect()->route( 'homepage' )->withSuccess(trans('app.login.success'));
}
return redirect()->back()->withErrors(trans('app.wrong.credentials'));
}

最佳答案

在 Laravel 5.3 中,您应该像这样更改 Controller 构造函数以使其工作(假设您至少使用 Laravel 5.3.4):

public function __construct()
{
$this->middleware(function ($request, $next) {
view()->share('usr', Auth::guard('user'));

return $next($request);
});
}

您可以在 Upgrade guide 中看到此更改。 :

In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4

关于laravel - Auth::guard ('user' )->user() 在 Laravel 5.3 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40613543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com