php Laravel,控制器中的 Auth::user()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25027996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:36:27  来源:igfitidea点击:

Laravel, Auth::user() in controller

phplaravel

提问by roy

Laravel framework- Why aren't I able to use Auth::user(), (to see if user has been logged in) in the controller of the laravel project. Is the Session not connected to the controller?

Laravel 框架 - 为什么我不能在 Laravel 项目的控制器中使用 Auth::user(),(查看用户是否已登录)。Session 没有连接到控制器吗?

HomeController.php

家庭控制器.php

 public function isauthorized(){
    if (Auth::user()){
        return View::make('home.basic')
            ->with('basic1', 'Authorized');
    }else{
        return View::make('home.basic')
            ->with('basic1', 'Not Authorized');
    }
}

My login system is working and the session is always set when logged in. When I call the function Auth::user() in the View or in routes.php, it is succesfull. But when I call the function in the controller, it is false. Help please.

我的登录系统正在运行,并且会话总是在登录时设置。当我在视图或 routes.php 中调用函数 Auth::user() 时,它是成功的。但是当我在控制器中调用该函数时,它是错误的。请帮忙。

回答by Zaerdna

The problem is that you haven't included the Auth facade.

问题是你没有包含 Auth 门面。

In your HomeController.php do:

在你的 HomeController.php 中:

use Illuminate\Support\Facades\Auth;

回答by Adam Marshall

If you're on Laravel 5.x, you'll probably have the auth middleware component. That might be the missing link.

如果您使用的是 Laravel 5.x,您可能会拥有 auth 中间件组件。那可能是缺失的环节。

I often use a pattern like this in my controllers:

我经常在我的控制器中使用这样的模式:

public function __construct()
    {
        $this->middleware('auth');
        $this->user =  \Auth::user();
    }

then you can

然后你可以

if ($this->user .. )

if ($this->user .. )

or

或者

if ($this->user->can('something'))with Entrust or similar

if ($this->user->can('something'))与委托或类似

回答by ozo

Just add the following in your controller library.

只需在您的控制器库中添加以下内容。

use Illuminate\Support\Facades\Auth;

回答by gxet4n

You need to use the Auth facade in the controller;

您需要在控制器中使用 Auth 门面;

add this line just before class definition.

在类定义之前添加这一行。

use Auth;

回答by Javi Stolz

To see if a user is loged in don't use Auth::user(). Use Auth::check()instead.

要查看用户是否已登录,请不要使用Auth::user(). 使用Auth::check()来代替。

if (Auth::check()){
    return View::make('home.basic')->with('user', Auth::user());
}

回答by Benubird

I have the same problem. The solution I've found, is to not use \Auth::user() (it returns null). Instead, go through the guard like this: \Auth::guard('mrm')->User(). Obviously replace 'mrm' with whatever auth guard you are using.

我也有同样的问题。我发现的解决方案是不使用 \Auth::user() (它返回 null)。取而代之的是,经过这样的后卫:\Auth::guard('mrm')->User()。显然,用您正在使用的任何身份验证保护替换“mrm”。

This works, correctly returning the current user even when Auth::User returns null, and you can also use other methods on it - e.g. \Auth::guard('mrm')->check()instead of \Auth::check()(which always returns false).

这有效,即使当 Auth::User 返回 null 时也能正确返回当前用户,并且您也可以在其上使用其他方法 - 例如\Auth::guard('mrm')->check()代替\Auth::check()(它总是返回 false)。

No idea why the Auth facade doesn't work, but at least there is an alternative that does.

不知道为什么 Auth 外观不起作用,但至少有一个替代方案。

回答by Gurpreet Singh

in your controllers constructor:

在您的控制器构造函数中:

public function __construct(){
    $this->middleware('auth');
}

回答by m4rinos

additionally, you can also use the auth()helper without adding the facade in your controller. (i.e. auth()->user()

此外,您还可以使用auth()帮助程序,而无需在控制器中添加外观。(IEauth()->user()

回答by MasterSith

Use Auth::check()function to check if user is logged or not

使用Auth::check()函数检查用户是否登录