【问题标题】:double slash in URLs.URL 中的双斜杠。
【发布时间】:2012-06-19 19:59:33
【问题描述】:

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+"

【问题讨论】:

    标签: zend-framework zend-route zend-acl zend-router


    【解决方案1】:

    您可以使用控制器插件来修复常见的 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"
    

    【讨论】:

    • 感谢 Zach - 为我解决了这个问题。我走错了路——我正在玩弄 apache 的 ReWrite 规则来删除双斜杠——但这一个很快很容易。谢谢
    • 另外请注意,如果您通过 boostrapper 中的配置文件使用 $router->addConfig($config, 'routes'); 提供路由器路由它实际上并没有添加已经存在的路由(默认值),而是覆盖它们..要真正添加它们,请确保您执行 $router->addDefaultRoutes();在你做 addConfig
    猜你喜欢
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2016-01-05
    • 2016-09-28
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多