我试图在Laravel 5.2.13中的事件处理程序中使用Auth::check()
。但是,当我执行dd(Auth::check())
时,即使我已登录,它也始终返回false。
如果我在控制器中执行dd(),它确实返回true并且我确实已登录。我的代码:
<?php namespace App\Listeners;
use App\Userpath;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
class UserEventListener
{
public function onNewPath($event)
{
dd(Auth::check()); // <--- always returns false!!
}
public function subscribe($events)
{
$events->listen(
'App\Events\NewPath',
'App\Listeners\UserEventListener@onNewPath'
);
}
}
我的路线
Route::group(['middleware' => 'web'], function () {
...
Route::group(['middleware' => ['termsandconditions', 'auth']], function () {
Event::fire(new NewPath()); //NewPath is just an empty default event
...
我的kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
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,
],
'api' => [
'throttle:60,1',
],
];
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
}
如果我是否登录,如何获得合法答案?我到处寻找SO,改变路线和中间件组,什么不是,但我不能让它工作。提前谢谢!
答案 0 :(得分:0)
您是如何将用户登录到您的应用程序的? 我建议您将仅用于$ midleware var的认证中间件添加到kernel.php midleware文件中。
答案 1 :(得分:0)
似乎命名空间错误,因为您已经App\Listeners
,所以它尝试使用App\Listeners\Auth
。尝试:
dd(\Auth::check());
或use Auth;
位于您的文件之上。