简体   繁体   English

EasyAdmin3 / Symfony / Twig - url 生成器,在 ZF35D7929DDF2FE2B7DE8 模板中具有路由名称和参数

[英]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.我正在尝试使用一些参数在我的模板中生成 EasyAdmin3 url,但由于某种原因,它们不存在于 controller 中。

Twig template: Twig 模板:

<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: 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 []. $_GET['id'] 有效,但请求和上下文为空 []。

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.如果您只是在 twig 中生成常规命名路由,我认为您不需要ea_url()助手 function。 You should be able to use the path() twig extension provided by Symfony.您应该能够使用 Symfony 提供的path() twig 扩展

{{ 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.如果您尝试创建指向 EasyAdmin controller 操作的链接,则可以使用ea_url()帮助程序,但您还必须指定 controller 和操作。 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...然后在你的 controller 中,一切都应该像往常一样通过$context变量提供......

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

This works for me, I hope it helps.这对我有用,我希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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