laravel - 开始升级 Laravel 应用程序后,自定义 Json Guard 不再起作用

标签 laravel laravel-5

我正在将继承的应用程序从 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有关但我无法完全理解中间件的实现,并且我对类型提示的尝试失败了。

有人建议从哪里开始吗?

最佳答案

Laravel 5.2 中不存在 Illuminate\Contracts\Session\Session 合约。 session 防护依赖于 Symfony 的 session interface .

JsonGuard中更改:

use Symfony\Component\HttpFoundation\Session\SessionInterface;

use Illuminate\Contracts\Session\Session as SessionInterface;

关于laravel - 开始升级 Laravel 应用程序后,自定义 Json Guard 不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64398601/

相关文章:

php - 我可以在 Laravel 中为 CI/CD 使用 php 部署器吗?

mysql - laravel,groupby()中的子字符串

laravel - "bindings"中间件在 Laravel 5.6 中有什么作用?

php - 使用 laravel 5 在谷歌云平台上配置 1and1 邮件

php - Eloquent-oauth-捕获PDO异常

laravel - 使用 Laravel/PHP 创建一个像 Wordpress 这样的基于插件的系统

javascript - 在 Laravel + AngularJS 中编辑

找不到 Laravel 4 播种类哨兵

php - 非默认守卫在 Laravel 5.5 中被忽略

php - PHP Laravel 查询生成器中的 SQL 语法 PostgreSQL 与 MySQL