我正在将继承的应用程序从 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/