Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10048

Symfony redirect url with pagination param

On my Symfony project I have this controller:

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

I also have this routes (using php way):

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

Whan I try to achieve is to redirect all requests eg. /list/1 into /list?page=1 but when I generate url from route need to generate the url from a route I need to set the url parameters after ? part.

DO you knwo how to do that?

Upvotes: 1

Views: 830

Answers (1)

Tomasz
Tomasz

Reputation: 5162

You can do it like that:

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

$this->redirect() will return RedirectResponse instance , and $this->generateUrl() will generate string based on your routing configuration.

So in your case this should work:

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

Upvotes: 1

Related Questions