Zend Framework中的主机名和自定义路由对我不起作用 [英] Hostname and Custom routing in Zend Framework don't work together for me

查看:89
本文介绍了Zend Framework中的主机名和自定义路由对我不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用主机名路由来检测子域的应用程序,例如

I am building an application that uses hostname routing to detect subdomains like

user1.example.com
user2.example.com

user1.example.com user2.example.com

,并且还具有自定义路由,例如user1.example.com/login

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));
}

当我走到 http://user1.example.com/login 我收到错误消息:指定的控制器无效(登录),这意味着我的自定义路由为不被认可。我也不确定我添加默认路由的方式是否正确和必要。如果我删除该代码,它将无法正常工作。所以我的问题确实是我希望我的主机名匹配,自定义路由和默认路由能够正常工作。如果您能找到我要去哪里的地方,请帮忙,我已经阅读了以前有关路由,链接,默认路由等的所有相关文章(包括与此非常相关的文章:如何在Zend框架中的子域中编写子域的路由链路由INI文件?),但到目前为止尚未找到解决方案。

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.

推荐答案

您应该可以进行设置您在路由中使用自定义参数的路由:

You should be able to setup your routing using a custom param in the route:

routes.testdynamicsubdomain.type = "Zend_Controller_Router_Route_Hostname"
routes.testdynamicsubdomain.route = ":subdomain.domain.dev"
routes.testdynamicsubdomain.defaults.module = public
routes.testdynamicsubdomain.defaults.controller = index
routes.testdynamicsubdomain.defaults.action = index

如果您的apache / hostfile等配置正确,将进行测试。 domain.dev应该将index操作加载到indexController中,您可以在其中获取:subdomain参数:

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:

echo $this->getRequest()->getParam('subdomain');

此外,正如您所发现的那样,路线的顺序也很重要。另请参阅 Zend路由器优先级,以了解有关此信息。

Also, as you discovered, the order of the routes is very important. See also Zend Router precedence for more info about this.

这篇关于Zend Framework中的主机名和自定义路由对我不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆