简体   繁体   English

如何在Symfony2中使用JavaScript生成其他页面的URL?

[英]How to generate URL for other pages using JavaScript in Symfony2?

Within my Symfony2 application, in order to link to other pages, I've generated the href value using the path Twig function . 在我的Symfony2应用程序中,为了链接到其他页面,我使用路径Twig函数生成了href值。

I maybe wrong but I think I previously read about a JavaScript equivalent but can't seem to find it now. 我可能错了,但是我想我以前读过关于JavaScript的文章,但现在似乎找不到。 I am generating some html using JavaScript and want to know if its possible to generate the href values to other pages within the application using purely JavaScript. 我正在使用JavaScript生成一些html,并想知道是否有可能使用纯JavaScript生成应用程序内其他页面的href值。

Appreciate any advice. 感谢任何建议。

You are probably looking for this bundle: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle 您可能正在寻找此捆绑包: https : //github.com/FriendsOfSymfony/FOSJsRoutingBundle

Then you can do this kind of stuff in javascript: 然后,您可以在javascript中进行此类操作:

Routing.generate('my_route', {"my":"param"});

I would recommend using FosJsRoutingBundle . 我建议使用FosJsRoutingBundle

Lets assume an action of controller: 让我们假设一个控制器的动作:

/**
 * @Route("/indx/", options={"expose"=true}, name="indexRoute")
 * @Template()
 *
 * @return array
 */
public function indexAction()
{
    (...)
}

options={"expose"=true} is what is important here. options={"expose"=true}在这里很重要。

Now js equivalent of path call is just: 现在相当于path调用的js只是:

Routing.generate('indexRoute');

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

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