Laravel 5.4 Auth外观不起作用。 auth中间件功能正常。按照预期,对保护路线的呼叫会产生登录页面。格式不正确的证书会产生预期错误。正如预期的那样,正确的凭据会生成正确的页面。注释掉该行“使用Illuminate \ Support \ Facades \ Auth;”如预期的那样产生错误“Class'App \ Http \ Controllers \ Admin \ Auth'not found”。laravel 5.4 auth外观不起作用
具体来说,即使登录成功,Auth :: check()也会产生false,并且Auth :: user()不会返回任何内容。调用Auth Facade的这些实现中的任何一个都不会产生错误。
此代码在Laravel 5.2中运行良好。我知道的程序唯一的偏差是在迁移到Laravel 5.4时,我没有在新的Laravel实现中运行“php artisan make:auth”命令。
搜索“laravel 5.4 auth facade不起作用”不会在Bing或stackoverflow内部搜索引擎中产生相关结果。
显示相关的代码,如下:
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Models\Admin;
use App\Models\Role;
use App\Models\Role_user;
use App\Models\Client;
use App\Models\Contact;
use App\Models\PublicPages;
use App\Models\Survey_question;
use App\Models\Survey_item;
use App\Models\Survey_project;
use App\Models\State;
use App\Models\Test_post;
use App\Models\Registration;
use Hash;
use Redirect;
use DB;
use Illuminate\Support\Facades\Auth;
use App\Classes\RoleHelper;
use App\Classes\CommonCode;
class AdminController extends Controller
{
var $obj_logged_in_user;
var $arr_logged_in_user;
var $bool_has_role;
var $roleHelper;
//organization of function
// 1. __construct
// 2. index
// get, alphabetically
// post, alphabetically
public function __construct(
Role_user $role_user, RoleHelper $roleHelper, Request
$request)
{
$this->middleware('auth');
echo "<pre>";
echo "admin controller, line 50<br>";
print_r($request->user);
echo "</pre>";
// line 49 (request->user) produces nothing
if (Auth::check())
{
$this->middleware('role:admin');
$this->obj_logged_in_user = Auth::user();
print_r($this->obj_logged_in_user);
// line 58 (print_r) produces nothing
$this->arr_logged_in_user = $roleHelper->prepare_logged_in_user_info($this->obj_logged_in_user);
} // end if Auth::check()
} // end __construct function
最后,我想为验证::检查()返回准确的结果,并验证::用户()返回登录的用户目的。先谢谢你。
您不能在构造函数中使用它,中间件甚至没有在此时运行。 – tkausl
会话未在中间件中设置,因此用户也不会从Auth门面中撤消。这不是使用控制器的正确方式,这是反模式的一个例子。 – Ohgodwhy