Symfony2路由

编程入门 行业动态 更新时间:2024-10-18 16:53:52
本文介绍了Symfony2路由-路由子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在 Symfony2 中设置基于主机名的路由?

Is there a way to set up hostname based routing in Symfony2?

在官方文档中我没有找到有关此主题的任何信息. symfony/doc/2.0/book/routing.html

I didn't find anything about this topic in the official documentation. symfony/doc/2.0/book/routing.html

我想基于给定的主机名路由请求: foo.example bar.example {{subdomain}}.example

I want to route the request based on the given hostname: foo.example bar.example {{subdomain}}.example

因此,从本质上讲,控制器将获得作为参数传递的当前子域.

So in essence, the controller would get the current subdomain passed as a parameter.

类似于Zend解决方案: framework.zend /manual/en/zend.controller.router.html#zend.controller.router.routes.hostname

Similar to the Zend solution: framework.zend/manual/en/zend.controller.router.html#zend.controller.router.routes.hostname

$hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.users.example', array( 'controller' => 'profile', 'action' => 'userinfo' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static(''); $router->addRoute('user', $hostnameRoute->chain($plainPathRoute));

我希望这是可能的,而我只是以某种方式错过了它. 预先感谢!

I hope that it's possible and I just missed it somehow. Thanks in advance!

推荐答案

这是我的解决方案:

在应用程序目录中的config.yml中,添加以下行:

In the config.yml inside app dir add the following lines:

services: kernel.listener.subdomain_listener: class: Acme\DemoBundle\Listener\SubdomainListener tags: - { name: kernel.event_listener, event: kernel.request, method: onDomainParse }

然后将类SubdomainListener.php创建为:

<?php namespace Acme\DemoBundle\Listener; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\Event; class SubdomainListener { public function onDomainParse(Event $event) { $request = $event->getRequest(); $session = $request->getSession(); // todo: parsing subdomain to detect country $session->set('subdomain', $request->getHost()); } }

更多推荐

Symfony2路由

本文发布于:2023-10-31 13:19:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!