Zend Framework 中的主机名和自定义路由对我来说不能一起工作
我正在构建一个使用主机名路由来检测
user1.example.com 等子域的应用程序 user2.example.com
也有自定义路由,例如 user1.example.com/login
到目前为止,这效果很好,但是当我添加自定义路由时,它们不起作用。我已经搜索和阅读了很多,但似乎缺少一些东西。这是我到目前为止所拥有的:
//my routes in routes.ini
[development]
routes.login.type = "Zend_Controller_Router_Route"
routes.login.route = "/login"
routes.login.defaults.controller = "user"
routes.login.defaults.action = "login"
//This part in Bootstrap file
$this->bootstrap('frontController');
$router = $this->frontController->getRouter();
$routerConfig = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/routes.ini',
'production'
);
//I create a default route
$routeDefault = new Zend_Controller_Router_Route_Module(
array(),
$this->frontController->getDispatcher(),
$this->frontController->getRequest()
);
$router->addConfig($routerConfig, 'routes');
// hostname route
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':username.mysite.com',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
)
);
//I add the default route.
$router->addRoute('default', $routeDefault);
//I chain the routes so that all routes have subdomain routing too
foreach ($router->getRoutes() as $key => $theroute) {
$router->addRoute($key, $hostnameRoute->chain($theroute));
}
当我进入像 http://user1.example.com/ 这样的自定义路线时登录 我收到错误:“指定的控制器无效(登录)”,这意味着我的自定义路由未被识别。我也不确定添加默认路由的方式是否正确和必要。如果我删除该代码,它就不起作用。所以我的问题实际上是我希望我的主机名匹配、自定义路由和默认路由都能正常工作。如果你能发现我哪里出错了,请帮忙,我已经阅读了之前所有关于路由、链接、默认路由等的相关帖子(包括这个非常相关的帖子:如何在 Zend Framework 中为子域编写路由链路由INI 文件?)但到目前为止还没有找到解决方案。
I am building an application that uses hostname routing to detect subdomains like
user1.example.com
user2.example.com
and also have custom routes like user1.example.com/login
This works well so far, however when I add custom routes they do not work. I have searched and read a lot but seems there is something I am missing. Here is what I have so far:
//my routes in routes.ini
[development]
routes.login.type = "Zend_Controller_Router_Route"
routes.login.route = "/login"
routes.login.defaults.controller = "user"
routes.login.defaults.action = "login"
//This part in Bootstrap file
$this->bootstrap('frontController');
$router = $this->frontController->getRouter();
$routerConfig = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/routes.ini',
'production'
);
//I create a default route
$routeDefault = new Zend_Controller_Router_Route_Module(
array(),
$this->frontController->getDispatcher(),
$this->frontController->getRequest()
);
$router->addConfig($routerConfig, 'routes');
// hostname route
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':username.mysite.com',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
)
);
//I add the default route.
$router->addRoute('default', $routeDefault);
//I chain the routes so that all routes have subdomain routing too
foreach ($router->getRoutes() as $key => $theroute) {
$router->addRoute($key, $hostnameRoute->chain($theroute));
}
When I go to a custom route like http://user1.example.com/login I get the error: 'Invalid controller specified (login)' which means my custom route is not being recognized. I am also not sure if the way I am adding the default route is correct and necessary. If I remove that code then it doesn't work. So my problem really is that I would like my hostname matching, custom routes and default routes to all work. If you can spot where I'm going wrong please help, I have read previous related posts all over on routes, chaining, default routes etc (including this very related one: How do I write Routing Chains for a Subdomain in Zend Framework in a routing INI file?) but haven't found the solution so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够在路由中使用自定义参数来设置路由:
如果您的 apache/hostfile 等配置正确,则转到 test.domain.dev 应该在您的 indexController 中加载索引操作,您可以在其中获取 :subdomain 参数:
另外正如您所发现的,路线的顺序非常重要。有关详细信息,另请参阅 Zend Router 优先级。
You should be able to setup your routing using a custom param in the route:
If your apache/hostfile etc are configured correctly going to test.domain.dev should load the index action in your indexController where you could get the :subdomain param:
Also, as you discovered, the order of the routes is very important. See also Zend Router precedence for more info about this.