laravel - 开始升级 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有关,但我无法完全理解中间件的实现,并且我尝试类型提示失败.
有没有人建议从哪里开始?
解决方案
Laravel Illuminate\Contracts\Session\Session
5.2 中不存在合约。会话守卫依赖于 Symfony 的会话接口。
JsonGuard
变化中:
use Symfony\Component\HttpFoundation\Session\SessionInterface;
至
use Illuminate\Contracts\Session\Session as SessionInterface;
推荐阅读
- javascript - 在 React 中将组件作为 prop 传递并将它们称为 JSX 标签
- vb.net - 由于最小尺寸,当表单调整大小时,表单子项不会调整大小
- azure-devops - 如何通过 Autho 2 详细信息白色在 Azure DevOps 中创建测试套件
- html - 带或不带关键帧的正方形顶部中心到底部中心的边框动画
- typescript - 为什么我可以调用一个用 instanceof 而不是 typeof 保护的函数?
- ocaml - Base 弃用了一些有用的函数
- c++ - 我收到运行时错误提示“运行时错误:引用绑定到空指针”
- themes - 如何为 CodeBlocks 更改或添加深色主题?
- three.js - 三个JS在知道原点和距离的平面上找到一个点
- python - urllib.error.HTTPError: HTTP Error 403: Forbidden in my web scraping