0

I have a scenario where the user clicks on a link received to an email which requires a sign in. if the link is http://test.url.com/product/2, the user should sign in and the page should redirect to the same path after authentication.

I tried to tackle this via the use of print_r($request->headers->get('referer')); which posted a question here : Redirect to the url of the clicked (referred) link . which was unsuccessful as it returned null.

My next aim is to pass parameters to the sign in page depending on the url.

eg: if the user clicks http://test.url.com/product/2, the sign in url would look like http://test.url.com/signin/product/2 or http://test.url.com/signing?=pruduct?2 or something like that where the system would redirect the user to the desired page.

Any document/ example on passing parameters from the config level is greatly appreciated as I am very new to this domain. thanks :)

Community
  • 1
  • 1
Hasitha Shan
  • 2,900
  • 6
  • 42
  • 83
  • Save the url in the cookie, and after authentication, redirect him – Cristian Bujoreanu Jan 12 '15 at 09:14
  • @CristianBujoreanu thank you very much for the reply. I will try this :) but is this the best way to do it? I am asking just to clarify as I am new to this field – Hasitha Shan Jan 12 '15 at 09:15
  • If your authentication process has more steps, can't use $request->headers->get('referer') anymore, so cookie solution would be very good, why not (don't forget to delete url from cookie). – Cristian Bujoreanu Jan 12 '15 at 09:18
  • @CristianBujoreanu thanks for that information :) i will try this and let you know (y) – Hasitha Shan Jan 12 '15 at 09:19
  • @CristianBujoreanu Hi..i was looking all over how to save the current url as a cooking within the security.yml.. if you are aware on how to this can you please give an example on how to get the current url and save it within the security.yml? thhanks :) – Hasitha Shan Jan 12 '15 at 11:03

2 Answers2

1

I would suggest to mention the redirect url as a query string on the login url like

http://test.url.com/signin/?redirect=http://test.url.com/product/2

Upon a successful login all you have to do is

$url=$_GET['redirect'];
header("location:".$url);
BiJ
  • 1,639
  • 5
  • 24
  • 55
  • Thank you very much :) ill check on this post back – Hasitha Shan Jan 12 '15 at 04:32
  • Hi, this method is great. just one thing, how can I access the current url of the page so that I can attach it to the `login_path: http://test.url.com/signin/?redirect=` in `security.yml`? thank you..i tried searching but ended up empty handed – Hasitha Shan Jan 12 '15 at 06:08
0

Does this helps you?

public function productAction($id, Request $request){
    $url = $this->container->get("router")->generate("product_route", array("id" => $id));

    /** @var $session Session */
    $session = $request->getSession();
    $session->set("redirect_url", $url);
}         

public function signinAction(Request $request){
    // do your stuff

    /** @var $session Session */
    $session = $request->getSession();
    if ($session->has("redirect_url")){
        return new RedirectResponse($session->get("redirect_url");
    }
}   
Cristian Bujoreanu
  • 1,147
  • 10
  • 21
  • Hi, as the redirection happens the application never reach a controller. so I must get the current url info within the security.yml or other suited yml files :) if this possible. – Hasitha Shan Jan 13 '15 at 04:18