Home > php教程 > PHP开发 > body text

Symfony2 creates domain name-based routing related examples

高洛峰
Release: 2016-12-26 12:32:17
Original
1169 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!