Zend默认路由掩码无效模块

时间:2012-03-24 09:11:07

标签: php zend-framework routes

在application.ini中我有默认路由:

resources.router.routes.default.type = "Zend_Controller_Router_Route"
resources.router.routes.default.route = ":lang/:module/:controller/:action/*"
resources.router.routes.default.defaults.module = "default"
resources.router.routes.default.defaults.controller = "index"
resources.router.routes.default.defaults.action = "index"
resources.router.routes.default.defaults.lang = ""

问题是,这似乎掩盖了无效模块上的404错误。例如,在像“/ en / mmm”这样的url上,请求被路由到default / index / index。但是对于“/ en / mmm / ccc”,触发了无效的控制器。

谢谢!

1 个答案:

答案 0 :(得分:0)

我可以说,这种行为是正常的。

我一直在查看文档和代码,似乎路由器会在/en/mmm这样的网址上加载默认控制器/操作。 lang:en有效,但mmm与有效模块或控制器不匹配,因此使用默认路由。

en/mmm/ccc的情况下,它会抛出404,因为在没有有效模块的情况下,路由将与控制器/操作匹配,并且因为值无效将导致404.我是确定为什么这导致404和无效的控制器只会导致调用默认路由。

此活动在Default Routes

的这些示例中得到了证明
 // Assuming the following: $ctrl->setControllerDirectory(
     array(
         'default' => '/path/to/default/controllers',
         'news'    => '/path/to/news/controllers',
         'blog'    => '/path/to/blog/controllers'
     ) );

 Module only: http://example/news
 //if not valid module would attempt to match controller.
 //if not valid controller would call default route
     module == news

 Invalid module maps to controller name: http://example/foo
     controller == foo

 Module + controller: http://example/blog/archive
     module     == blog
     controller == archive

 Module + controller + action: http://example/blog/archive/list
 //all three must be valid or 404
     module     == blog
     controller == archive
     action     == list

 Module + controller + action + params:
 http://example/blog/archive/list/sort/alpha/date/desc
     module     == blog
     controller == archive
     action     == list
     sort       == alpha
     date       == desc

以下代码摘录似乎是我发现演示此行为的最佳示例。

//excerpt from Zend_Controller_Router_Route_Module

if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
            if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
                $module = $params[$this->_moduleKey];
            }
        }
        unset($params[$this->_moduleKey]);

        $controller = $params[$this->_controllerKey];
        unset($params[$this->_controllerKey]);

        $action = $params[$this->_actionKey];
        unset($params[$this->_actionKey]);

        foreach ($params as $key => $value) {
            $key = ($encode) ? urlencode($key) : $key;
            if (is_array($value)) {
                foreach ($value as $arrayValue) {
                    $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
                    $url .= self::URI_DELIMITER . $key;
                    $url .= self::URI_DELIMITER . $arrayValue;
                }
            } else {
                if ($encode) $value = urlencode($value);
                $url .= self::URI_DELIMITER . $key;
                $url .= self::URI_DELIMITER . $value;
            }
        }

        if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
            if ($encode) $action = urlencode($action);
            $url = self::URI_DELIMITER . $action . $url;
        }

        if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
            if ($encode) $controller = urlencode($controller);
            $url = self::URI_DELIMITER . $controller . $url;
        }

        if (isset($module)) {
            if ($encode) $module = urlencode($module);
            $url = self::URI_DELIMITER . $module . $url;
        }

        return ltrim($url, self::URI_DELIMITER);
    }

希望这有帮助。