Home php教程 PHP开发 Symfony2 creates domain name-based routing related examples

Symfony2 creates domain name-based routing related examples

Dec 26, 2016 pm 12:32 PM

The example of this article describes the implementation method of Symfony2 creating routing based on domain name. Share it with everyone for your reference, the details are as follows:

You can match incoming requests in HTTP domain name

YAML

mobile_homepage:
 path:  /
 host:  m.example.com
 defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
homepage:
 path:  /
 defaults: { _controller: AcmeDemoBundle:Main:homepage }
Copy after login

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>
Copy after login

PHP method

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add(&#39;mobile_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
), array(), array(), &#39;m.example.com&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
return $collection;
Copy after login

Two Routes match the same path / , however the first one will only match the domain name m.example.com

uses placeholders

This domain option uses the placeholder path matching system. This means you can use placeholder matching domain names in your domain names.

YAML

projects_homepage:
 path:  /
 host:  "{project_name}.example.com"
 defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
homepage:
 path:  /
 defaults: { _controller: AcmeDemoBundle:Main:homepage }
Copy after login

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>
Copy after login

PHP

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add(&#39;project_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
), array(), array(), &#39;{project_name}.example.com&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
return $collection;
Copy after login

You can also set conditions and default options for these placeholders. For example, if you want to match m.example.com and mobile.example.com you can do it as follows

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 }
Copy after login

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>
Copy after login

PHP

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add(&#39;mobile_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
 &#39;subdomain&#39; => &#39;m&#39;,
), array(
 &#39;subdomain&#39; => &#39;m|mobile&#39;,
), array(), &#39;{subdomain}.example.com&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
return $collection;
Copy after login

You can also use service parameters if you don’t want to The hard-written domain name is as follows

YAML

mobile_homepage:
 path:  /
 host:  "m.{domain}"
 defaults:
  _controller: AcmeDemoBundle:Main:mobileHomepage
  domain: &#39;%domain%&#39;
 requirements:
  domain: &#39;%domain%&#39;
homepage:
 path: /
 defaults: { _controller: AcmeDemoBundle:Main:homepage }
Copy after login

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>
Copy after login

PHP

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add(&#39;mobile_homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:mobileHomepage&#39;,
 &#39;domain&#39; => &#39;%domain%&#39;,
), array(
 &#39;domain&#39; => &#39;%domain%&#39;,
), array(), &#39;m.{domain}&#39;));
$collection->add(&#39;homepage&#39;, new Route(&#39;/&#39;, array(
 &#39;_controller&#39; => &#39;AcmeDemoBundle:Main:homepage&#39;,
)));
return $collection;
Copy after login

Tip

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

Use included routing rule matching

You can set domain name options by importing routing configuration files as follows

YAML

acme_hello:
 resource: &#39;@AcmeHelloBundle/Resources/config/routing.yml&#39;
 host:  "hello.example.com"
Copy after login

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>
Copy after login

PHP

use Symfony\Component\Routing\RouteCollection;
$collection = new RouteCollection();
$collection->addCollection($loader->import("@AcmeHelloBundle/Resources/config/routing.php"), &#39;&#39;, array(), array(), array(), &#39;hello.example.com&#39;);
return $collection;
Copy after login

Domain name hello .example.com will be set for each route in the new route configuration file that is loaded

Test your Controllers

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

$crawler = $client->request(
 &#39;GET&#39;,
 &#39;/homepage&#39;,
 array(),
 array(),
 array(&#39;HTTP_HOST&#39; => &#39;m.&#39; . $client->getContainer()->getParameter(&#39;domain&#39;))
);
Copy after login

I hope this article will be helpful to everyone’s PHP programming based on the Symfony2 framework .

For more related examples of Symfony2 creating routing based on domain name, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)