0

Zend 路由问题。

通常它工作正常。

http://www.example.com/course-details/1/Physics-Newtons-Law

但是如果我在 url 中输入一个额外的斜杠,我的错误控制器的 noauthAction 就会被调用。

无效的 URL 示例。

http://www.example.com/course-details//1/Physics-Newtons-Law
http://www.example.com/course-details/1//Physics-Newtons-Law

我需要在路由定义中设置什么以允许额外的斜杠吗?

application.ini 中的路由

resources.router.routes.viewcourse.route = "/course-details/:course_id/:title"
resources.router.routes.viewcourse.defaults.controller = 当然
resources.router.routes.viewcourse.defaults.action = 查看
resources.router.routes.viewcourse.defaults.title =
resources.router.routes.viewcourse.reqs.course_id = "\d+"
4

1 回答 1

2

您可以使用控制器插件来修复常见的 URL 拼写错误。

/**
 * Fix common typos in URLs before the request
 * is evaluated against the defined routes.
 */
class YourNamespace_Controller_Plugin_UrlTypoFixer 
    extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup($request)
    {
        // Correct consecutive slashes in the URL.
        $uri = $request->getRequestUri();
        $correctedUri = preg_replace('/\/{2,}/', '/', $uri);
        if ($uri != $correctedUri) {
            $request->setRequestUri($correctedUri);
        }
    }
}

然后在你的ini文件中注册插件。

resources.frontController.plugins.UrlTypoFixer = "YourNamespace_Controller_Plugin_UrlTypoFixer"
于 2012-06-17T17:16:11.550 回答