在默认 Zend 路由中添加可选参数
我想更改应用程序上的默认路由,以通过 Bootstrap.php 使用可选参数,而不是在 application.ini 中。我在设置所有“逃生”路线时遇到困难,我什至不知道是否需要设置几条路线,或者我是否只能设置一条路线!
路由必须类似于
":module/:area/:controller/:action"
其中 :area 是可选的并且默认为“public”
因此在对 http 的任何访问中://www.example.com/my_module/my_controller/my_action 我可以通过 FC 插件获取以下内容
$request->getParam('area') = "public"
: href="http://www.example.com/my_module/my_area/my_controller/my_action" rel="nofollow">http://www.example.com/my_module/my_area/my_controller/my_action 我明白:
$request->getParam('area') = "my_area"
主要的麻烦是创建所有路由,以便它转义到 default 模块、index 控制器和 index 操作,以防它们也没有传递并加载<一href="http://www.example.com/" rel="nofollow">http://www.example.com/ 返回与 http://www.example.com/default/public/index/index。
我想使用 URL Helper 生成内部 URL,并且它尊重这个新默认路由上的正确程序集。
编辑:我认为默认路由带有模块的故障保护,如果它不存在,则默认为控制器,但是在创建时如何模仿 URL 的其他部分和模块本身的类似行为它在引导程序中。
任何人都可以给我一些启发或建议我在哪里可以找到工作示例吗?
I want to change the default route on my application to use an optional parameter via Bootstrap.php, not in the application.ini. I am having trouble setting up all "escape" routes, I even don't know If I need to set up several routes or if I can do with only one!
The route must be something like
":module/:area/:controller/:action"
where :area is optional and defaulted to "public"
So in any access to http://www.example.com/my_module/my_controller/my_action I can get via a FC plugin the following:
$request->getParam('area') = "public"
And in http://www.example.com/my_module/my_area/my_controller/my_action I get:
$request->getParam('area') = "my_area"
The main trouble is creating all routes so it escapes to default module, index controller and index action in case those are not passed too and loading http://www.example.com/ returns same as http://www.example.com/default/public/index/index.
And I want to use URL Helper to generate internal URL´s and it respects the correct assembly on this new Default Route.
Edit: I figured that default Route comes with a fail-safe for the module, if it don't exist it defaults to the controller, but how to mimic similar behavior for other parts of the URL and the module itself when creating it in the Bootstrap.
Can anybody give me some light or suggest where can I find an working example ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以扩展
Zend_Controller_Router_Rewrite
并更改默认路由器设置和 URL 组装,然后在 Zend_Controller_Front 上使用->setRouter()
来使用新的路由器实例而不是默认的。搜索class Zend_Controller_Router_Rewrite
并在执行此操作之前学习它。You can extend
Zend_Controller_Router_Rewrite
and change the default router settings and URL assembling, and then use->setRouter()
on your Zend_Controller_Front to use your new router instance instead of the default. Search forclass Zend_Controller_Router_Rewrite
and learn it before doing so.解决方案确实是扩展 Zend_Controller_Router_Route_Abstract 并创建一个新的 Route 处理程序来遍历 url,验证模块、区域、控制器和操作是否存在,以构建完整功能的 url 或分派到错误控制器。
繁重的工作是在
match()
和assemble()
方法之间完成的,我使用 Zend 类Zend_Controller_Router_Route_Module
作为参考,它执行类似的任务模块,但不是通过调度程序验证模块是否存在,而是创建了允许区域的白名单,如果无法匹配,则认为该路径部分是控制器,下一个操作和其他部分是参数。它就像魔术一样起作用!
The solution was indeed to extend
Zend_Controller_Router_Route_Abstract
and create a new Route handler to go through the url, verify the existence of modules, areas, controllers and actions to build a full functional url or dispatch to the error controller.The heavy work is done between the
match()
andassemble()
methods and I used as reference the Zend classZend_Controller_Router_Route_Module
tht do similar task for the module, but instead of verifying if module exists with the dispatcher I created a whitelist of allowed areas, if it fails to match, it considers that that path part is the controller, the next the action and further parts are parameters.It worked like magic!