Symfony2创建基于域名的路由相关示例
框架(架构)  /  管理员 发布于 7年前   179
本文实例讲述了Symfony2创建基于域名的路由实现方法。分享给大家供大家参考,具体如下: 你可以匹配将要来到的请求以HTTP域名的方式 YAML方式 XML方式 PHP方式 两个路由匹配相同的路径 / ,然而第一个将只有域名为m.example.com才匹配 使用占位符 这个域名选项使用占位符的路径匹配系统。这样就意味着你可以在你的域名中使用占位符匹配的域名。 YAML XML PHP 你还可以为这些占位符设置条件和默认的选项。列如,如果你想匹配 m.example.com 和mobile.example.com你可以按照如下方式 YAML XML PHP 你还可以使用服务参数,如果你不想将域名写死写法如下 YAML XML PHP 提示 确保你总是包含了默认的选项 domain占位符,否则你需要包含 domain的值每当你使用该路由生成URL的时候。 使用包含进来的路由规则匹配 你可以设置域名选项通过导入路由配置文件,方式如下 YAML XML PHP 域名 hello.example.com 将要被设置为加载进来的新路由配置文件中的每个路由 测试你的Controllers 你需要设置HTTP的域名头文件在你请求的对象中,如果你想正确的匹配到网址在你的测试函数中 更多关于Symfony2相关内容感兴趣的读者可查看本站专题:《Symfony框架入门教程》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家基于Symfony2框架的PHP程序设计有所帮助。mobile_homepage: path: / host: m.example.com defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
use Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route;$collection = new RouteCollection();$collection->add('mobile_homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',), array(), array(), 'm.example.com'));$collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage',)));return $collection;
projects_homepage: path: / host: "{project_name}.example.com" defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
use Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route;$collection = new RouteCollection();$collection->add('project_homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',), array(), array(), '{project_name}.example.com'));$collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage',)));return $collection;
mobile_homepage: path: / host: "{subdomain}.example.com" defaults: _controller: AcmeDemoBundle:Main:mobileHomepage subdomain: m requirements: subdomain: m|mobilehomepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
use Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route;$collection = new RouteCollection();$collection->add('mobile_homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', 'subdomain' => 'm',), array( 'subdomain' => 'm|mobile',), array(), '{subdomain}.example.com'));$collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage',)));return $collection;
mobile_homepage: path: / host: "m.{domain}" defaults: _controller: AcmeDemoBundle:Main:mobileHomepage domain: '%domain%' requirements: domain: '%domain%'homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }
use Symfony\Component\Routing\RouteCollection;use Symfony\Component\Routing\Route;$collection = new RouteCollection();$collection->add('mobile_homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', 'domain' => '%domain%',), array( 'domain' => '%domain%',), array(), 'm.{domain}'));$collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage',)));return $collection;
acme_hello: resource: '@AcmeHelloBundle/Resources/config/routing.yml' host: "hello.example.com"
use Symfony\Component\Routing\RouteCollection;$collection = new RouteCollection();$collection->addCollection($loader->import("@AcmeHelloBundle/Resources/config/routing.php"), '', array(), array(), array(), 'hello.example.com');return $collection;
$crawler = $client->request( 'GET', '/homepage', array(), array(), array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain')));
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号