0

I have a route

 detail: path: /{code} defaults: { _controller: controller.main:detailAction } 

I also have a controller for this route

 public function detailAction(Request $request, string $code, int $size, array $params): Response { } 

My question is: how can I say to controller which parameters he should take as int $size and array $params ? I have found in symfony docs that I may specifically mention params in defaults section with default values like this

 detail: path: /{code} defaults: { _controller: controller.main:detailAction } size: 1 params: "Hello world!" 

But that is not what I want since I shouldn't have a default value for this params but it ought to be taken directly from request. How do I do this without making my route like /{code}/{size} ? And even in this case what do I do with an array?

5
  • Are you saying sometimes there's just a code, sometimes a code + size, and sometimes a code + size + params? Commented Jun 23, 2017 at 12:16
  • no, as my detailAction() statement requires all arguments, it always must be all parameters passed. But only $code is part of the path, others I just want to have passed Commented Jun 23, 2017 at 12:21
  • I would make the arguments optional: detailAction(..., int $size = null, array $params = null). Then you can test if they're set, but I don't believe (?) Symfony's router will see those as required arguments. Commented Jun 23, 2017 at 12:45
  • So like: 3v4l.org/iOLga Commented Jun 23, 2017 at 12:45
  • This question makes no sense. Commented Jun 23, 2017 at 13:28

2 Answers 2

3

You can generate a url like this by passing parameters in your controller:

 $url = $this->generateUrl("detail", array("code" => $code, ...)); return $this->redirect($url); 

And routing:

 detail: path: / defaults: { _controller: controller.main:detailAction } 
Sign up to request clarification or add additional context in comments.

2 Comments

In symfony >= 3.3 you can directly do return $this->redirectToRoute('detail', array('code' => $code....));
is it the only way? Can't I state in my routing that some parameters should be clearly passed (size -> $size etc)?
0

If you want to specify parameters like this

someurl.io/action/?filter=allopenissues&orderby=created 

You should typehint Request object in your action and access its query parameters bag. If your controller extends Symfony Controllers, Request will be automatically passed.

use Symfony\Component\HttpFoundation\Request; .... public function updateAction(Request $request) { $request->query->get('myParam'); // get myParam from query string } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.