linuxatico
linuxatico

Reputation: 1896

Advanced routing parameter handling in twig and Symfony2

I have this configuration:

A page containing a search field, at the submit in the same page I want a list of every result matching the research, everyone linking to a corresponding route. For example if I find 4 elements, I want that in the resulting page 4 links Azienda1, Azienda2 ecc.

Now I get this error:

An exception has been thrown during the rendering of a template ("The "ABCAziendaBundle_visualizza_azienda" route has some missing mandatory parameters ("id_azienda").") in ::base.html.twig at line 27. 500 Internal Server Error - Twig_Error_Runtime 1 linked Exception: MissingMandatoryParametersException

Here are the key files,

#config.yml
ABCAziendaBundle_visualizza_azienda:
    pattern: /visualizza_azienda/{id_azienda}
    defaults: { _controller: ABCAziendaBundle:Default:showAzienda }
    requirements:
    id_azienda: \d+

ABCAziendaBundle_azienda_index:
    pattern:  /
    defaults: { _controller: ABCAICAziendaBundle:Default:indexAzienda }

#DefaultController.php

public function indexAziendaAction(Request $request) {

    $searchFormType = new SearchAziendaType();
    $form = $this->createForm($searchFormType);

    if ($request->getMethod() == 'POST')
    {
        $form->bindRequest($request);
        if ($form->isValid())
        {
            $data = $form->getData();

            $em = $this->getDoctrine()->getEntityManager();
            $aziende = $em->getRepository('ABCAziendaBundle:Azienda')->findAziendaByAliasOrRagioneSocialeSubstring($data["search_field"]);
            return $this->render('ABCAziendaBundle:Default:indexAzienda.html.twig', array('form' => $form->createView(), 'aziende' => $aziende));
        }
    }

    return $this->render('ABCAziendaBundle:Default:indexAzienda.html.twig', array('form' => $form->createView()));
}

public function showAziendaAction($id_azienda) {

    echo "non entra qui";
}

#indexAzienda.html.twig
{% extends "::base.html.twig" %}

{% block pagetitle %}ABC{% endblock %}

{% block body %}
<h2>Ricerca azienda</h2>
<div id="form_container">
    <form action="{{ path('ABCAziendaBundle_azienda_index') }}" method="post" {{ form_enctype(form) }}>
        {{ form_label(form.search_field, "Ricerca Azienda") }}
        {{ form_widget(form.search_field) }}
            <input type="submit" />
    </form>
    <a href="{{ path('ABCAziendaBundle_registra_azienda_anagrafica') }}"><button>nuova azienda</button></a>
</div>
    {% if aziende is defined %}
        {% for azienda in aziende %}
            <div class="areaTot">
                <a href="{{ path('ABCAziendaBundle_visualizza_azienda', { 'azienda_id' : azienda.id })  }}">{{azienda.alias}}</a>
            </div>
        {% endfor %}
    {% else %}
    <div class="areaTot">
        <p>"NIENTE"</p>
    </div>
    {% endif %}
{% endblock %}

Upvotes: 2

Views: 4448

Answers (1)

Matt
Matt

Reputation: 10823

I think its a typo on your side:

<a href="{{ path('ABCAziendaBundle_visualizza_azienda', { 'azienda_id' : azienda.id })  }}">

Should be:

<a href="{{ path('ABCAziendaBundle_visualizza_azienda', { 'id_azienda' : azienda.id })  }}">

The difference is the route parameter, you wrote azienda_id initialy, but the route parameter name is id_azienda

Should clear the error.

Regards, Matt

Upvotes: 3

Related Questions