1

I discovering symfony3, but im stuck at getting the parameters passed with a link from an action. In my twig file, im redirecting the user to an action:

<td><a href="{{ path('esprit_park_affiche', {'id': voiture.id, 'serie': voiture.serie, 'dateMise': voiture.dateMiseCirculation, 'marque': voiture.marque}) }}" target="_blank">go to</a></td>

And im my action, i'm trying to get the id with:

/**
 * @Route("/AfficheDetail", name="esprit_park_affiche")
 */
public function afficheAction()
{
    $id = $this->getParameter("id");

    return $this->render("@EspritPark/Voiture/affiche.html.twig", array("id" => $id));
}

but each time i get: The parameter "id" must be defined. like the getParameter isnt returning anything.

I even tried with:

$id = $this->get("request")->get("id");

but i get: You have requested a non-existent service "request". Did you mean one of these: "monolog.logger.request", "request_stack", "router.request_context", "data_collector.request"?

1 Answer 1

4

The getParameter() method from the base Controller class is looking up parameters from the service container.

I would make the parameters part of your route. You can then retrieve the values through the action method's parameters:

/**
 * @Route("/AfficheDetail/{id}/{serie}/{dateMise}/{marque}", name="esprit_park_affiche")
 */
public function afficheAction($id, $serie, $dateMise, $marque)
{
    // ...
}

If you do not add them to the route pattern, they will be accessible through the URL parameters (the current request will be injected automatically if you type hint an argument with the Request class):

public function afficheAction(Request $request)
{
    $id = $request->query->get('id');
    $serie = $request->query->get('serie');
    $dateMise = $request->query->get('dateMise');
    $marque = $request->query->get('marque');

    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.