Dinuka Thilanga
Dinuka Thilanga

Reputation: 4330

Symfony 1.4 - Pass URL in get request

I want to redirect the previous page after the login. My first question is what is the best solution for it?

I try following solution. send previous page url in get request and redirect to the url that in get request.

This is my previous page action.

public function executeVotes(sfWebRequest $request)
{
        $chaletId = (int) $request->getParameter('id');
        $this->rates = (int) $request->getParameter('rates', 0);

        if (
            ($this->getUser()->isAuthenticated() === FALSE )
            || ($this->getUser()->hasCredential('member_permission') === FALSE)
        ) {
            $this->redirect('member_signin', array(
                'redirect' => $this->getController()->genUrl('chalet_votes/' . $chaletId . '/?rates=' . $this->rates, true)
            ));
        }
}

This is my signin action

public function executeSignin(sfWebRequest $request)
{
        $this->redirectUrl = $this->getRequest()->getParameter('redirect');

        echo $this->redirectUrl; die;
}

This is routers for these actions.

member_signin:
  url: /member/login/*
  param: { module: member, action: signin}

votes:
  url: /chalet/votes/:id
  param: { module: test, action: votes }

This is url after redirect to the signin page.

http://test.dev/member/login/redirect/http%3A%2F%2Fchalet.dev%2Fchalet_votes%2F1%2Frates%2F9

My server show 404 page error. What is the issue? How can i do it?

Upvotes: 2

Views: 1510

Answers (2)

Amit Kriplani
Amit Kriplani

Reputation: 682

I think this should work.

$this->redirect('member_signin', array(
    'redirect' => urlencode($this->getController()->genUrl('chalet_votes/' . $chaletId . '/?rates=' . $this->rates, true))
));

Note the urlencode(). So dont forget to use urldecode() on url before redirecting.

You can also use session to save url, but remember to unset it from session once redirected after login to prevent redirect loop.

Hope this helps.

Upvotes: 1

antony
antony

Reputation: 2893

Instead of redirecting the non-authenticated user to the member_signin. Try forwarding them:

$this->forward('member_signin');

Because you're not redirecting, when you post the sign-in form, you will have the referrer URL available in the $request->getReferer() method.

So what you'll need to do in your signin action:

public function executeSignin(sfWebRequest $request)
{
    $user = $this->getUser();

    // If the user has been authenticated, helps prevent redirect loop as well.
    if ($user->isAuthenticated()) {
        $this->redirect('@defaultpage');
    }

    // Do authentication
    //...

    // If authenticated
    $this->redirect($request->getReferer());
}

Upvotes: 3

Related Questions