简体   繁体   中英

EasyAdmin3 / Symfony / Twig - url generator with route name and params in Twig template

I'm trying to generate EasyAdmin3 url inside my template with some params, but for some reason they are not present in a controller.

Twig template:

<a href="{{ ea_url().setRoute('route_name').set('id', 1) }}">xxx</a>

<a href="{{ ea_url({'routeName': 'route_name', 'id':1}) }}">yyy</a>

Error with missing EA context:
<a href="{{ path('route_name', {'id': 1}) }}">zzz</a>

Controller:

/**
 * @Route("/admin/something/{id}", name="rounte_name")
 */
public function xyz($id = null, Request $request, AdminContext $context)
{
    dd($_GET['id'], $request->request->all(), $context->getRequest()->request->all());
    ...
}

The $_GET['id'] works, but request and context are empty [].

Any idea how to generate route by name with params?

Thanks

I don't think you need the ea_url() helper function if you are just generating regular named routes in twig. You should be able to use the path() twig extension provided by Symfony.

{{ path(route_name, route_parameters = [], relative = false) }}

If you are trying to create a link to an EasyAdmin controller action, then you can use the ea_url() helper, but you have to specify the controller and action as well. Try something like:

{% set url = ea_url()
                .setController('App\\Controller\\Admin\\FoobarCrudController')
                .setAction('customFooAction')
                .setEntityId(entity.instance.id)
                .set('myParam', 'baz') %}
<a href="{{ url }}">Custom Foobar Action</a>

Then in your controller, everything should be available as per usual via the $context variable...

public function customFooAction(AdminContext $context)
{
    $entity = $context->getEntity()->getInstance();
    $myParam = $context->getRequest()->get('myParam');
}

This works for me, I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM