Zend - 基于参数数量的路由规则

发布于 2025-01-07 20:30:42 字数 832 浏览 0 评论 0原文

在我的项目中,我使用 zend 来处理路由。 Atm 我们有如下所示的路由规则: 大批( '匹配' => '页', '参数' =>数组('页面','idConfiguration'), '控制器' => '控制器1', '行动' => '行动1' )

因此,我们通过以下方式访问此操作: http://base_url/page/1/1223324 例如。

是否有一个简单的解决方案来创建规则,以便我可以根据参数数量确定调用哪个操作?

我希望它看起来像下面这样: http://base_url/ - 操作 0 http://base_url/pageNumber - 操作 1 http://base_url/pageNumber/idConfiguration - 操作 2 http://base_url/pageNumber/idConfiguration/someotherparam - 操作 3

提前感谢您的帮助

in my project I'm using zend to handle routing. Atm we've got routing rules which looks like this:
array(
'match' => 'page',
'params' => array('page', 'idConfiguration'),
'controller' => 'controler1',
'action' => 'action1'
)

So we access this action by: http://base_url/page/1/1223324 for example.

Is there a simple solution to create rules so i can determine which action is called based on number of params?

I'd like it to look the following way:
http://base_url/ - action 0
http://base_url/pageNumber - action 1
http://base_url/pageNumber/idConfiguration - action 2
http://base_url/pageNumber/idConfiguration/someotherparam - action 3

Thank you in advance for help

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

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

发布评论

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

评论(2

花间憩 2025-01-14 20:30:43

基于ini的解决方案(希望我明白你想要什么):

routes.action0.route = "/:pageNumber"
routes.action0.defaults.controller = "controller0"
routes.action0.defaults.action = "action0"
routes.action0.reqs.pageNumber = "\d+"

routes.action1.route = "/:pageNumber/:idConfiguration"
routes.action1.defaults.controller = "controller1"
routes.action1.defaults.action = "action1"
routes.action1.reqs.pageNumber = "\d+"
routes.action1.reqs.idConfiguration= "\d+"

routes.action2.route = "/:pageNumber/:idConfiguration/:someOtherParam"
routes.action2.defaults.controller = "controller2"
routes.action2.defaults.action = "action2"
routes.action2.reqs.pageNumber = "\d+"
routes.action2.reqs.idConfiguration= "\d+"
routes.action2.reqs.someOtherParam = "someOtherRegEx"

Ini-based solution (hope I understand what you want):

routes.action0.route = "/:pageNumber"
routes.action0.defaults.controller = "controller0"
routes.action0.defaults.action = "action0"
routes.action0.reqs.pageNumber = "\d+"

routes.action1.route = "/:pageNumber/:idConfiguration"
routes.action1.defaults.controller = "controller1"
routes.action1.defaults.action = "action1"
routes.action1.reqs.pageNumber = "\d+"
routes.action1.reqs.idConfiguration= "\d+"

routes.action2.route = "/:pageNumber/:idConfiguration/:someOtherParam"
routes.action2.defaults.controller = "controller2"
routes.action2.defaults.action = "action2"
routes.action2.reqs.pageNumber = "\d+"
routes.action2.reqs.idConfiguration= "\d+"
routes.action2.reqs.someOtherParam = "someOtherRegEx"
咿呀咿呀哟 2025-01-14 20:30:43

您可以子类 Zend_Controller_Router_Route< /a> 并创建您喜欢的路由行为。在我的脑海中,无需测试,您就可以尝试这样的事情:

class MyRoute extends Zend_Controller_Router_Route
{
    public function match($path) 
    {
        $result = array(
            'controller' => 'index',
            'action' => substr_count($path, '/'),
        );

        return $result;
    } 
}

当然,您需要添加验证。此外,如果 URL 不匹配并且您希望使用其他路由对其进行测试,则应该返回 FALSE。但这应该让您对如何解决这个问题有一个大致的了解。

You can subclass Zend_Controller_Router_Route and create the routing behaviour you like. Of the top of my head, and without testing it you can try something like this:

class MyRoute extends Zend_Controller_Router_Route
{
    public function match($path) 
    {
        $result = array(
            'controller' => 'index',
            'action' => substr_count($path, '/'),
        );

        return $result;
    } 
}

You would need to add validations, of course. Also you should return FALSE if the URL doesn't match and you want it to be tested with other routes. But this should give you a general idea on how to work this out.

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