Zend 和 addRoute() 的问题

发布于 2024-10-08 00:55:53 字数 1458 浏览 3 评论 0原文

使用此代码:

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin'
        )
    )
);

http://app/test/param1/param2/param3 ->确定

http://app/test/param1/param2/ ->失败

在第二种情况下,应用程序无法识别 param2。

似乎应用程序需要 param3 才能读取 param2...

我该怎么做?

谢谢!


中的代码进行测试

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

使用 @RageZ http://app/test/ -> 好的

http://app/test/some ->好的

http://app/test/some/more ->失败

http://app/test/some/more/andmore ->好

主意吗?

With this code:

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin'
        )
    )
);

http://app/test/param1/param2/param3 -> OK

http://app/test/param1/param2/ -> FAIL

In the second case the application don't recognize param2.

It seems that the application needs the param3 in order to read the param2...

How can I do it?

Thanks!


Test with the code from @RageZ

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

http://app/test/ -> OK

http://app/test/some -> OK

http://app/test/some/more -> FAIL

http://app/test/some/more/andmore -> OK

Ideas?

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

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

发布评论

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

评论(2

清晰传感 2024-10-15 00:55:53

尝试为所有内容指定默认值

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'action' => 'index',
            'type' => 'sometype',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

刚刚在一些测试项目中尝试了您的代码,这修复了它希望它对您有用!

try giving a default value to everything

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'action' => 'index',
            'type' => 'sometype',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

Just tried your code in some test project and this fixed it hope it works for you!

土豪我们做朋友吧 2024-10-15 00:55:53

如果参数是可选的,则必须提供默认值。

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

与您的问题无关,但使用 addRoute 的第三个参数是一个很好的做法。 Zend Framework 将验证参数值是否与您指定的格式匹配,在这种情况下,我认为 id 是一个整数。

You have to provide a default value if the parameter is optional.

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

Nothing to do with your question but it's a good practice to use the third param of addRoute. Zend Framework would verify that the parameters value match the format you have specified, in that case I suppose id is an integer.

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