symfony 1.4:基于部分 url 的路由

发布于 2024-12-23 18:46:26 字数 311 浏览 3 评论 0原文

我正在尝试执行以下操作:

http://www.mydomain.com/Foo/json_bar

在我的路由文件中,我想说任何转到 Foo/json_* 的内容,它应该转到 action.class.php 文件中的相应操作,

例如:
Foo/json_bar1 ->;公共函数executeBar1 Foo/json_bar2 ->;公共函数executeBar2

谢谢

I am trying to do the following:

http://www.mydomain.com/Foo/json_bar

in my routing file, I want to say anything going to Foo/json_* it should go to the appropriate action in the action.class.php file

ex:
Foo/json_bar1 -> public function executeBar1
Foo/json_bar2 -> public function executeBar2

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2024-12-30 18:46:26

在这种情况下,您可能可以编写这样的路由规则(未经测试):

my_rule:
  url:   /Foo/json_:action/
  params: { module: myModule, sf_method: json }

This,因为 :action 参数是一个“神奇”参数,它设置操作。 (通常您在 params 块中设置 action 参数。

顺便说一句,sf_method 是可选的,但它将请求格式设置为 json这样,任何异常也将以 JSON 形式呈现,并且为 json 设置正确的标头。

顺便说一下,执行此操作的最佳实践是:
我的规则:
url: /Foo/:action.:sf_method
params: { module: myModule }

在这种情况下,您可以编写 bar1 操作。转到 /Foo/bar1.html 将呈现 HTML,而 /Foo/bar1.json 将呈现 json 响应。当然,您可以随意将 :sf_method 替换为 json,并设置 sf_method 参数,就像我的第一个示例中一样。

In that case, you could probably write a routing rule like this (untested):

my_rule:
  url:   /Foo/json_:action/
  params: { module: myModule, sf_method: json }

This, because the :action parameter is a "magic" parameter, which sets the action. (Normally you set the action parameter in the params block.

The sf_method is optional, by the way, but it sets the request format as json. That way, any exceptions will also render in JSON, and the correct headers are set for json.

The best practice to do this by the way, would be:
my_rule:
url: /Foo/:action.:sf_method
params: { module: myModule }

In that case you can write a bar1 action. Going to /Foo/bar1.html will render the HTML, and /Foo/bar1.json will render a json response. Of course you're free the replace the :sf_method with json, and set the sf_method param, like in my first example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文