我有这个全局中间件,可以更新用户的上一次活动
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Closure;
class HandleLastActivity
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (auth()->user()) {
$user = auth()->user();
$user->lastActivity = now();
$user->save();
}
return $next($request);
}
}
,但是auth()-> user()始终为null。我正在使用Sanctum,它可以在auth中间件下的路由中工作。
env
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_DOMAIN=.mydomain.com
SANCTUM_STATEFUL_DOMAINS=mydomain.com,www.mydomain.com,api.mydomain.com
config / auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
],
],