2017-01-03 7 views
0

私のsymfony3のログインページは、私のsecurity.ymlファイルに記載されているように、デフォルトでは家にリダイレクトされます。ユーザーの状態でログインした後のsymfonyのリダイレクト

しかし、私が「プロフィールの編集」ページにリダイレクトするのは、ユーザーがまだそれを実行しなかった場合です。私はコントローラでこれを作っていますが、ログインフォームには$form->handleRequest($user)がないので、私は$userのテスト変数を持っていません。

はロールに基づいてユーザーをリダイレクトする方法についてSO話題がたくさんあり、そしてdocumentationは、フォームのアクションフィールドからまたはsecurity.yml以内にリダイレクトについて伝え、しかしいないが、私が探しているものです。

条件に基づいてリダイレクトするにはどうすればよいですか?

NB:。いくつかの理由のために、私は

+0

:ログインのようなものだろう

security: firewalls: main: pattern: ^/ anonymous: ~ form_login: login_path: login check_path: login default_target_path: login_success always_use_default_target_path: true logout: path: logout 

。 html – davidbonachera

+0

これは私があなたが知っていたこととまったく同じリンクです... –

+0

おっと...セッション/クッキーを使用して空であればリダイレクトする方法があります。 http://symfony.com/doc/current/components/http_foundation.html#setting-cookies/http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/Cookie.html – davidbonachera

答えて

0

:-(まだFOSUserBundleを使用することはできません私はあなたがガード認証システムを使用していると仮定(http://symfony.com/doc/current/security/guard_authentication.html

その後、あなたはAbstractGuardAuthenticatorクラスを拡張するクラスを持っている必要があります。そのクラスで

は、onAuthenticationSuccessと呼ばれる方法がここにあなたが要求をリダイレクトするためのいくつかのロジックを置くことができ、そこにある。 あなたがここにnullを返した場合、それだけで継続、およびセキュリティに設定したルートを使用します。 yml。

@routerサービスは、dependencyInjectionを通じてAuthenticatorクラスに渡す必要があります。あなたはいつも、単に仲介ルートを導入して行うことができます他のすべてが失敗した(またはに対処するため、過剰な痛みになります)場合

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) 
{ 
    /** @var User $user */ 
    $user = $token->getUser(); 

    if ($user->hasCompleteProfile() == false) { 
     $url = $this->router->generate('edit_profile'); 

     return new RedirectResponse($url); 
    } 

    // Continue with default behaviour 
    return null; 
} 
+0

実際、私は[a Simple Registration Form](https://symfony.com/doc/current/doctrine/registration_form.html)、[従来のログインフォーム](https://symfony.com/doc/current/security/form_login_setup.html) )。私はガード認証システムを調べますが、私が構築しているウェブサイトはロック・ハード・セキュリティを持っているわけではないので、おそらくそれは過剰です... –

0

:あなたの方法は次のようになります、あなたは、ルータのサービスを渡されたと仮定すると、

そこにあなたのロジック。

つまり、必要なロジックに基づいてユーザーをリダイレクトし、それをターゲットパスとしてsecurity.ymlファイアウォールに配置するという唯一の目的を持つアクションを作成します。 http://symfony.com/doc/current/security/form_login:あなたはそのようなカスタム入力してフォーム内でリダイレクトすることができます

class AuthenticationController extends Controller 
{ 
    /** 
    * @Route("/login", name="login") 
    * @Route("/logout", name="logout") 
    */ 
    public function loginAction(Request $request) 
    { 
     // Standard login stuff here 
    } 

    /** 
    * @Route("/login_success", name="login_success") 
    */ 
    public function postLoginRedirectAction() 
    { 
     if (/* user needs to see location A */) { 
      return $this->redirectToRoute("location_a"); 
     } else if (/* user needs to see location B */) { 
      return $this->redirectToRoute("location_b"); 
     } else { 
      return $this->redirectToRoute("index"); 
     } 
    } 
} 
関連する問題