简体   繁体   English

在默认的Zend路由中添加可选参数

[英]Add a optional parameter in default Zend route

I want to change the default route on my application to use an optional parameter via Bootstrap.php, not in the application.ini. 我想在我的应用程序上更改默认路由,以通过Bootstrap.php使用可选参数,而不是在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" 其中:area是可选的,默认为“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: 因此,在访问http://www.example.com/my_module/my_controller/my_action时,我可以通过FC插件获取以下内容:

$request->getParam('area') = "public"

And in http://www.example.com/my_module/my_area/my_controller/my_action I get: http://www.example.com/my_module/my_area/my_controller/my_action中,我得到:

$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 . 主要的麻烦是创建所有路由,以便它们转移到默认模块, 索引控制器和索引操作,以防它们也没有通过,并加载http://www.example.com/返回相同的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. 我想使用URL Helper生成内部URL,并且它尊重这个新的默认路由上的正确程序集。

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. 编辑:我认为默认路由为模块提供了故障保护,如果它不存在则默认为控制器,但是如何在URL中创建它时模拟URL和模块本身的其他部分的类似行为引导。

Can anybody give me some light or suggest where can I find an working example ? 任何人都可以给我一些启示或建议我在哪里可以找到一个有效的例子?

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. 您可以扩展Zend_Controller_Router_Rewrite并更改默认路由器设置和URL组装,然后在Zend_Controller_Front上使用->setRouter()来使用新的路由器实例而不是默认路由器实例。 Search for class Zend_Controller_Router_Rewrite and learn it before doing so. 搜索class Zend_Controller_Router_Rewrite并在此之前学习它。

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. 解决方案确实是扩展Zend_Controller_Router_Route_Abstract并创建一个新的Route处理程序来遍历url,验证是否存在模块,区域,控制器和操作以构建完整的功能URL或调度到错误控制器。

The heavy work is done between the match() and assemble() methods and I used as reference the Zend class Zend_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. match()assemble()方法之间完成繁重的工作,我用作Zend类Zend_Controller_Router_Route_Module作为参考,为模块执行类似的任务,但不是验证模块是否与调度程序一起存在,而是创建了允许区域的白名单,如果它不匹配,则认为该路径部分是控制器,下一个动作和其他部分是参数。

It worked like magic! 它像魔术一样工作!

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

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