2016-07-01 10 views
0

は、私はカスタムSessionGuardを作成するにはどうすればよいIlluminate\Auth\SessionGuardカスタムRecaller Laravel

/** 
* Get the name of the cookie used to store the "recaller". 
* 
* @return string 
*/ 
public function getRecallerName() { 
    return 'remember_'.$this->name.'_'.sha1(static::class); 
} 

にコードを見つけるremember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d

myprefix_web_59ba36addc2b2f9401580f014c7f58ea4e30989dにデフォルトのクッキー変更したいですか?誰かが私を助けることができますか?

答えて

0

SessionGuardに建てられたが、これを変更する方法がありませんので、あなたはメソッドをオーバーライドする独自のGuardクラスを作成する必要があり、そしてあなたのGuardクラスを使用するAuthを教えてくれます。この情報はmy answer hereで説明されており、TokenGuardのカスタマイズ方法について説明しています。

まず、SessionGuardクラスを拡張する新しいGuardクラスを作成します。新しい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はそれについて知っている必要があります。

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; 
    }); 
} 

をそして最後に、あなたはあなたの新しいmysessionガードを使用するようにAuthを伝える必要があります:あなたは、あなたのAuthServiceProviderサービスプロバイダにboot()方法でこれを行うことができます。これはconfig/auth.php設定ファイルで行われます。

'guards' => [ 
    'web' => [ 
     'driver' => 'mysession', 
     'provider' => 'users', 
    ], 
], 
関連する問題