由于内置的SessionGuard 没有办法改变这一点,您需要创建自己的Guard 类来覆盖该方法,并告诉Auth 使用您的Guard 类。 my answer here 中也说明了此信息,其中说明了如何自定义 TokenGuard。
首先,首先创建一个新的Guard 类,该类扩展了基础SessionGuard 类。在您的新 Guard 类中,您将覆盖 getRecallerName() 方法以返回您想要的名称。在本例中,它创建于app/Services/Auth/MySessionGuard.php:
namespace App\Services\Auth;
use Illuminate\Auth\SessionGuard;
class MySessionGuard extends SessionGuard
{
/**
* Get the name of the cookie used to store the "recaller".
*
* @return string
*/
public function getRecallerName()
{
return 'myprefix_'.$this->name.'_'.sha1(static::class);
}
}
创建课程后,您需要通知Auth。您可以在 AuthServiceProvider 服务提供商的 boot() 方法中执行此操作:
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
Auth::extend('mysession', function($app, $name, array $config) {
$provider = $this->createUserProvider($config['provider']);
$guard = new \App\Services\Auth\MySessionGuard($name, $provider, $app['session.store']);
// When using the remember me functionality of the authentication services we
// will need to be set the encryption instance of the guard, which allows
// secure, encrypted cookie values to get generated for those cookies.
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($app['cookie']);
}
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($app['events']);
}
if (method_exists($guard, 'setRequest')) {
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));
}
return $guard;
});
}
最后,你需要告诉Auth 使用你的新mysession 守卫。这是在config/auth.php 配置文件中完成的。
'guards' => [
'web' => [
'driver' => 'mysession',
'provider' => 'users',
],
],