【问题标题】:Symfony 1.4 - Pass URL in get requestSymfony 1.4 - 在获取请求中传递 URL
【发布时间】:2013-05-15 09:21:10
【问题描述】:

我想在登录后重定向上一页。我的第一个问题是什么是最好的解决方案?

我尝试以下解决方案。 在 get 请求中发送上一页 url 并重定向到 get 请求中的 url。

这是我之前的页面操作。

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

这是我的登录操作

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

        echo $this->redirectUrl; die;
}

这是用于这些操作的路由器。

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

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

这是重定向到登录页面后的 url。

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

我的服务器显示 404 页面错误。问题是什么?我该怎么做?

【问题讨论】:

  • 为什么不将之前的 URL 存储到 SESSION 变量中???

标签: php symfony1 symfony-1.4


【解决方案1】:

而不是将未经身份验证的用户重定向到member_signin。尝试转发它们:

$this->forward('member_signin');

因为您没有重定向,所以当您发布登录表单时,您将在 $request->getReferer() 方法中获得推荐人 URL。

那么您在登录操作中需要做什么:

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

【讨论】:

    【解决方案2】:

    我认为这应该可行。

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

    注意urlencode()。所以在重定向之前不要忘记在 url 上使用urldecode()

    您也可以使用 session 来保存 url,但记住在登录后重定向后从 session 中取消设置,以防止重定向循环。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 2022-11-14
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多