Zend_Controller_Router_Route_Hostname和端口 ZendPHPF#
程序员文章站
2022-07-14 17:36:58
...
最近在配置zend的路由,需要根据不同的二级域名进行权限控制。其中使用到了Zend_Controller_Router_Route_Hostname这个路由。并使用了路由链,配置代码如下:
routes.hostname.type = "Zend_Controller_Router_Route_Hostname" routes.hostname.route = ":instance" routes.hostname.chains.static.route = ":action" routes.hostname.chains.static.defaults.controller = index routes.hostname.chains.objects.type = "Zend_Controller_Router_Route_Regex" routes.hostname.chains.objects.route = "([a-z]+(?:-[a-z]+)?)s(?:/([a-z]+)?)?" routes.hostname.chains.objects.defaults.action = list routes.hostname.chains.objects.map.1 = "controller" routes.hostname.chains.objects.map.2 = "action" routes.hostname.chains.objects.reverse = "%ss/%s" routes.hostname.chains.object.type = "Zend_Controller_Router_Route_Regex" routes.hostname.chains.object.route = "([a-z]+(?:-[a-z]+)?)s/([0-9a-f]{32})(?:/([a-z]+))?" routes.hostname.chains.object.defaults.action = view routes.hostname.chains.object.map.1 = "controller" routes.hostname.chains.object.map.2 = "id" routes.hostname.chains.object.map.3 = "action" routes.hostname.chains.object.reverse = "%ss/%s/%s" routes.hostname.chains.children.type = "Zend_Controller_Router_Route_Regex" routes.hostname.chains.children.route = "((?:[a-z]+s/[1-9]\d*/)*)([a-z]+(?:-[a-z]+)?)s/([0-9a-f]{32})/([a-z]+(?:-[a-z]+)?)s(?:/([a-z]+))?" routes.hostname.chains.children.defaults.action = list routes.hostname.chains.children.map.1 = "ancestors" routes.hostname.chains.children.map.2 = "parent_cls" routes.hostname.chains.children.map.3 = "parent_id" routes.hostname.chains.children.map.4 = "controller" routes.hostname.chains.children.map.5 = "action" routes.hostname.chains.children.reverse = "%s%ss/%s/%ss/%s"
发现程序中生成的url的端口号全部丢失。仔细看代码源码发现Zend_Controller_Router_Route_Chain中的assemble方法
public function assemble($data = array(), $reset = false, $encode = false) { $value = ''; foreach ($this->_routes as $key => $route) { if ($key > 0) { $value .= $this->_separators[$key]; } $value .= $route->assemble($data, $reset, $encode); if (method_exists($route, 'getVariables')) { $variables = $route->getVariables(); foreach ($variables as $variable) { $data[$variable] = null; } } } return $value; }
循环调用路由,但是在Zend_Controller_Router_Route_Hostname中的match方法中
// Get the host and remove unnecessary port information $host = $request->getHttpHost(); if (preg_match('#:\d+$#', $host, $result) === 1) { $host = substr($host, 0, -strlen($result[0])); }
这段去掉了端口号,而Zend_Controller_Router_Route_Chain中有将这里返回的url与后面的url拼接,这样产生的url端口自然就丢失了。不知道当时作者为什么要这样写,是不是因为有其他的原因?返回带端口的url有何不可?希望对PHP熟悉的人给予解释。
下一篇: JavaScript 中的接口实现