首页 > 解决方案 > 开始升级 Laravel 应用后,自定义 Json Guard 不再起作用

问题描述

我正在将继承的应用程序从 Laravel 5.2 升级到 5.4(希望继续使用最新版本)但是我遇到了 JSON Guard 不再工作的错误。在对 v5.4 进行更改后,此代码在 v5.2 中工作(生产中)我现在得到Argument 3 passed to App\Services\Auth\JsonGuard::__construct() must be an instance of Symfony\Component\HttpFoundation\Session\SessionInterface, instance of Illuminate\Session\Store given

这是 JsonGuard __construct:

namespace App\Services\Auth;
 
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\StatefulGuard;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
 
class JsonGuard implements StatefulGuard
{
      /**
       * Create a new authentication guard.
       *
       * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
       * @param  \Symfony\Component\HttpFoundation\Session\SessionInterface  $session
       * @param  \Symfony\Component\HttpFoundation\Request  $request
       * @return void
       */
      public function __construct($name,
                                  UserProvider $provider,
                                  SessionInterface $session,
                                  Request $request) {
        $this->name = $name;
        $this->session = $session;
        $this->request = $request;
        $this->provider = $provider;
      }
     

这是 AuthServiceProvider

use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\Services\Auth\JsonGuard;
use App\Extensions\OptimumHQUserProvider;
use App\Database\OptimumHQDatabase;
use App\Models\Auth\User;
// use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Config;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);

        $this->app->bind('App\Database\OptimumHQDatabase', function ($app) {
          return new OptimumHQDatabase(config('optimumhq.defaults.host'), config('optimumhq.defaults.username'), config('optimumhq.defaults.token'));
        });
         
        $this->app->bind('App\Models\Auth\User', function ($app) {
          return new User($app->make('App\Database\OptimumHQDatabase'));
        });
     
        // add custom guard provider
        Auth::provider('optimumhq', function ($app, array $config) {
          return new OptimumHQUserProvider($app->make('App\Models\Auth\User'));
        });
     
        // add custom guard
        Auth::extend('json', function ($app, $name, array $config) {
          return new JsonGuard('json', Auth::createUserProvider($config['provider']), $this->app['session.store'], $app->make('request'));
        });
    }

错误是$this->app['session.store']是“store”的实例,但我不知道如何将其更改为 SessionInterface 的实例或如何将其一直跟踪到哪里它最初是设置的。

我“认为”如果我可以将$this->app['session.store']更改为 SessionInterface 它将起作用。我非常不确定为什么它在 5.2 中有效但不再有效。似乎它可能与https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors有关,但我无法完全理解中间件的实现,并且我尝试类型提示失败.

有没有人建议从哪里开始?

标签: laravellaravel-5

解决方案


Laravel Illuminate\Contracts\Session\Session5.2 中不存在合约。会话守卫依赖于 Symfony 的会话接口

JsonGuard变化中:

use Symfony\Component\HttpFoundation\Session\SessionInterface;

use Illuminate\Contracts\Session\Session as SessionInterface;

推荐阅读