【问题标题】:Symfony redirect url with pagination param带有分页参数的 Symfony 重定向 url
【发布时间】:2017-11-22 11:59:26
【问题描述】:

在我的 Symfony 项目中,我有这个控制器:

class MyListController extends Controller
{
  public function myListRedirectAction($page)
  {
    //How I will set ? params
    $url=$this->generateUrl('my_list');
    $response=new RedirectResponse($url);

    return $response;
  }

  public function myListAction()
  {
    $paginationIndex=$request->query->get('page');
    //Get List
  }
}

我也有这个路由(使用php方式):

$list=new Route('/list',array(
    '_controller'=>'AppBundle:MyList:myList'
));
$collection->add('my_list',$list);


$listRedirect=new Route('/list/{page}',array(
    '_controller'=>'AppBundle:MyList:myListRedirect'
),array(
  'page'=>'\d+'
));
$collection->add('my_list_redirect',$list);

我试图实现的是重定向所有请求,例如。 /list/1 变成 /list?page=1 但是当我从路由生成 url 需要从路由生成 url 我需要在 ? 部分之后设置 url 参数。

你知道怎么做吗?

【问题讨论】:

    标签: php symfony redirect routes symfony-3.3


    【解决方案1】:

    你可以这样做:

    return $this->redirect($this->generateUrl('route_name', ['params']));
    

    $this->redirect() 将返回 RedirectResponse instance ,$this->generateUrl() 将根据您的路由配置生成字符串。

    所以在你的情况下这应该有效:

    return $this->redirect($this->generateUrl('my_list_redirect', ['page' => 1]));
    

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      相关资源
      最近更新 更多