Relate twig template with php controller in symfony

2

I have a method in the controller used to change data in a table.

public function bajaServicioAction($idservicio, $idcliente) 
{
  codigo para hacer el cambio en la base de datos
}

On the other hand I have a template ficha.html.twig where I want to put a link to execute the aforementioned function

<a href="{{ path('clientes_baja_servicio', {'idservicio': servicio.grupo, 
'idcliente': cliente.codigo}) }}"><i class="glyphicon glyphicon-remove-circle"></i></a>

Obviously it does not work because I do not know how to do it correctly, to pass that data to the controller function.

In summary, I need to pass some data from the twig to the function bajaServicioAction but I do not know how to

We can say that the route is configured to relate these two things, but I do not know how to pass the data from one to the other.

I edit to mention another important detail.

I do not want to go to that route, if not to execute the action that is located in it, that is to say that recharge the current page with the data.

    
asked by Pavlo B. 12.03.2018 в 17:59
source

1 answer

1

I hope to understand what your controller is doing, I understand that you want to cancel a service using a link, no?

If so, you have to keep in mind that the process of creating a controller has 3 parts, the controller, the path and the template.

The controller: This one is correct.

The route: Here I think this is where you have the problem, you have to define the route using replaceable values for the values you want to reach your controller, it would be something like this:

# config/routes.yaml
clientes_baja_servicio:
    path:      /baja-servicio/{idservicio}/{idcliente}
    controller: XXXX\Controller\XXXXXController::bajaServicio
    requirements:
       idservicio: '\d+'
       idcliente: '\d+'

You have to replace the XXXX with its corresponding one and I'm assuming the path of the controller, the requirements validate that the entries have a correct format, but you can remove them if you want.

The template: I think you have it right, the twig impression of the route should work.

I hope your comments help me, just as I leave you two links that could complement my explanation:

link

link

Greetings

    
answered by 26.03.2018 в 15:50