2017-07-28 108 views
1

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 

最后,我想为验证::检查()返回准确的结果,并验证::用户()返回登录的用户目的。先谢谢你。

+1

您不能在构造函数中使用它,中间件甚至没有在此时运行。 – tkausl

+0

会话未在中间件中设置,因此用户也不会从Auth门面中撤消。这不是使用控制器的正确方式,这是反模式的一个例子。 – Ohgodwhy

回答

0

如果您想在__construct()方法中使用Auth函数,那么您需要将它传递给闭包中的$this->middleware()

public function __construct(Role_user $role_user, RoleHelper $roleHelper) 
{ 
    $this->middleware('auth'); 
    $this->middleware('role:admin'); 
    $this->middleware(function ($request, $next) use ($role_user, $roleHelper) { 

     if (Auth::check()) { 

      $this->obj_logged_in_user = Auth::user(); 
      $this->arr_logged_in_user = $roleHelper->prepare_logged_in_user_info($this->obj_logged_in_user); 

     } 

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

而且,只是一个仅供参考,您可能会发现它更容易使用的dump()dd()功能附带Laravel。而是写:

echo "<pre>"; 
echo "admin controller, line 50<br>"; 
print_r($request->user); 
echo "</pre>"; 

可以转而写:

dump('admin controller, line 50', $request->user); 

https://laravel.com/docs/5.4/helpers#method-dd

希望这有助于!

+0

但是,如果您设置了auth中间件,它将在用户未登录时重定向用户。是否有任何方法检查用户是否已登录,而不自动将其重定向? – Benubird

+0

@Benubird只是不包括'$ this->中间件('auth');'位(最有可能的角色:admin'位)。 –

+0

如果你离开中间件,那么Auth :: user返回null,Auth :: check返回false – Benubird