symfony 1.4:基于部分 url 的路由
我正在尝试执行以下操作:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您可能可以编写这样的路由规则(未经测试):
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):
This, because the
:action
parameter is a "magic" parameter, which sets the action. (Normally you set theaction
parameter in theparams
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
withjson
, and set thesf_method
param, like in my first example.