Example of creating a domain name-based route using Symfony2 and symfony2

Source: Internet
Author: User
Tags subdomain

Example of creating a domain name-based route using Symfony2 and symfony2

This example describes how to create a domain name-based route for Symfony2. We will share this with you for your reference. The details are as follows:

You can match the request to be sent to an HTTP domain name.

YAML Method

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="http://symfony.com/schema/routing" 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

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;

The two routes match the same path/. However, the first one will only match the domain name m.example.com.

Use placeholders

This domain name option uses the placeholder path to match the system. This means that you can use a placeholder matching domain name in your domain name.

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="http://symfony.com/schema/routing" 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. If you want to match m.example.com and mobile.example.com, follow these steps:

YAML

mobile_homepage: path:  / host:  "{subdomain}.example.com" defaults:  _controller: AcmeDemoBundle:Main:mobileHomepage  subdomain: m requirements:  subdomain: m|mobilehomepage: path:  / defaults: { _controller: AcmeDemoBundle:Main:homepage }

XML

<?xml version="1.0" encoding="UTF-8" ?><routes xmlns="http://symfony.com/schema/routing" 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 service parameters. If you do not want to write the domain name 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="http://symfony.com/schema/routing" 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;

Prompt

Make sure that you always include the default option domain placeholder, otherwise you need to include the domain value whenever you use this route to generate a URL.

Match with included routing rules

You can set the domain name option to import the route configuration file as follows:

YAML

acme_hello: resource: '@AcmeHelloBundle/Resources/config/routing.yml' host:  "hello.example.com"

XML

<?xml version="1.0" encoding="UTF-8" ?><routes xmlns="http://symfony.com/schema/routing" 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;

The domain name hello.example.com will be set as each route in the loaded new route configuration file

Test your Controllers

You need to set the HTTP domain name header file in the object you requested, if you want to match the URL correctly in your test function

$crawler = $client->request( 'GET', '/homepage', array(), array(), array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain')));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.