Laravel全球中间件已检测到但未更改模型记录

时间:2018-10-13 03:31:06

标签: laravel middleware laravel-5.7

我有以下中间件(LastSeen):

<?php

namespace App\Http\Middleware;

use Closure;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\Factory as Auth;

class LastSeen
{
     /**
     * The authentication factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
     protected $auth;

     /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
     public function __construct(Auth $auth)
     {
         $this->auth = $auth;
     }
     /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
         if ($this->auth->check()) {
             $user = $this->auth->user();
             $user->last_activity = Carbon::now();
             $user->save();
        }
        return $next($request);
    }
}

这主要来自此答案(Check if user online laravel),是我正在从事的项目的权宜之计。

我的users表中确实有一个TIMESTAMP列,标记为last_activity,这是我的内核文件全局中间件:

/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \App\Http\Middleware\LastSeen::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
    \App\Http\Middleware\ModifyHeadersMiddleware::class,
];

我不确定下一步该怎么做,我只是开始使用中间件,并且仍在学习,但不确定我在做什么错...

谢谢!

2 个答案:

答案 0 :(得分:1)

问题在于有关授权用户的信息存储在会话中,并且LastSeen的身份验证检查在会话开始之前正在运行,因此始终返回false。

您需要在StartSession中间件之后移动LastSeen中间件:

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,

        \App\Http\Middleware\LastSeen::class,

        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

答案 1 :(得分:0)

有一些问题,但是主要的错误是您调用了错误的Auth。您必须调用立面,您正在调用Auth工厂。另一件事是,避免使用构造函数。把事情简单化。特别是对于MiddleWare。最后一件事,您正在更新用户状态,而不是保存新条目。

namespace App\Http\Middleware;

use Closure;
use Carbon\Carbon;
use Auth;

class LastSeen
{
     /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
         if (Auth::check()) {
             $user = Auth::user();
             $user->last_activity = Carbon::now();
             $user->update();
        }
        return $next($request);
    }
}