The example in this article describes how Symfony2 creates a domain-based routing implementation. Share to everyone for your reference, specific as follows:
You can match the request that will come to the HTTP domain name in the way
Yaml Way
Mobile_homepage:
path: /
Host: m.example.com
defaults: {_controller:acmedemobundle:main: Mobilehomepage}
Homepage:
path: /
defaults: {_controller:acmedemobundle:main:homepage}
XML method
<?xml version= "1.0" encoding= "UTF-8"?> <routes "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://symfony.com/schema/routing
Http://symfony.com/schema/routing/routing-1.0.xsd ">
<route id=" mobile_homepage "path="/"host=" M.example.com ">
<default key=" _controller ">AcmeDemoBundle:Main:mobileHomepage</default>
</route>
<route id= "homepage" path= "/" >
<default key= "_controller" >acmedemobundle:main: homepage</default>
</route>
</routes>
PHP Way
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;
Two routes match the same path/, but the first will only match the domain name m.example.com
Using placeholders
This domain name option uses a placeholder for the path matching system. This means that you can use placeholder matching domain names in your domain.
Yaml
Projects_homepage:
path: /
Host: "{project_name}.example.com"
defaults: {_controller: AcmeDemoBundle:Main:mobileHomepage}
Homepage:
path: /
defaults: {_controller:acmedemobundle : Main:homepage}
Xml
<?xml version= "1.0" encoding= "UTF-8"?> <routes "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://symfony.com/schema/routing
Http://symfony.com/schema/routing/routing-1.0.xsd ">
<route id=" projects_homepage "path="/"host=" { Project_name}.example.com ">
<default key=" _controller ">acmedemobundle:main:mobilehomepage</ default>
</route>
<route id= "homepage" path= "/" >
<default key= "_controller" > acmedemobundle:main:homepage</default>
</route>
</routes>
Php
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;
You can also set conditions and default options for these placeholders. Lieru, if you want to match m.example.com and mobile.example.com you can follow the following way
Yaml
Mobile_homepage:
path: /
Host: "{subdomain}.example.com"
defaults:
_controller: AcmeDemoBundle:Main:mobileHomepage
subdomain:m
requirements:
subdomain:m|mobile
homepage:
path: /
defaults: {_controller:acmedemobundle:main:homepage}
Xml
<?xml version= "1.0" encoding= "UTF-8"?> <routes "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://symfony.com/schema/routing
Http://symfony.com/schema/routing/routing-1.0.xsd ">
<route id=" mobile_homepage "path="/"host=" { Subdomain}.example.com ">
<default key=" _controller ">acmedemobundle:main:mobilehomepage</default >
<default key= "subdomain" >m</default>
<requirement key= "subdomain" >m|mobile</ requirement>
</route>
<route id= "homepage" path= "/" >
<default key= "_controller" >AcmeDemoBundle:Main:homepage</default>
</route>
</routes>
Php
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;
You can also use the service parameters if you do not want to write the domain name to death as follows
Yaml
Mobile_homepage:
path: /
Host: "M.{domain}"
defaults:
_controller:acmedemobundle : main:mobilehomepage
domain: '%domain% '
requirements:
domain: '%domain% '
homepage:
path :/
defaults: {_controller:acmedemobundle:main:homepage}
Xml
<?xml version= "1.0" encoding= "UTF-8"?> <routes "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://symfony.com/schema/routing http ://symfony.com/schema/routing/routing-1.0.xsd ">
<route id=" mobile_homepage "path="/"host=" M.{domain} " >
<default key= "_controller" >AcmeDemoBundle:Main:mobileHomepage</default>
<default Key = "Domain" >%domain%</default>
<requirement key= "domain" >%domain%</requirement>
< /route>
<route id= "homepage" path= "/" >
<default key= "_controller" >acmedemobundle:main: homepage</default>
</route>
</routes>
Php
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;
Tips
Make sure you always include the default option domain placeholder, otherwise you need to include domain values whenever you use that route to generate URLs.
Match with the included routing rules
You can set the domain name option by importing the routing profile in the following way
Yaml
Acme_hello:
resource: ' @AcmeHelloBundle/resources/config/routing.yml '
host: "hello.example.com"
Xml
<?xml version= "1.0" encoding= "UTF-8"?> <routes "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://symfony.com/schema/routing http ://symfony.com/schema/routing/routing-1.0.xsd ">
<import resource=" @AcmeHelloBundle/resources/config/ Routing.xml "host=" hello.example.com "/>
</routes>
Php
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;
Domain name hello.example.com is to be set to each route in the new routing configuration file that is loaded in
Test your controllers.
You need to set the HTTP header file in the object you requested, if you want to correctly match the URL in your test function
$crawler = $client->request (
' get ',
'/homepage ',
Array (),
array (), Array (
' Http_host ' => ' M. '. $client->getcontainer ()->getparameter (' domain '))
;
More interested in Symfony2 related content readers can view the site topics: "Symfony Framework Introductory Course", "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP Excellent Development Framework Summary", " thinkphp Getting Started tutorial, thinkphp Common methods Summary, Zend Framework Framework Introduction Tutorial, PHP object-oriented Program Introduction tutorial, Php+mysql database operation Getting Started tutorial and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design based on SYMFONY2 framework.