From f96aef0ff83dc7f3b134d1b802c4a577050ed77c Mon Sep 17 00:00:00 2001 From: Victor Bocharsky Date: Tue, 25 Apr 2017 13:08:28 +0300 Subject: [PATCH] Add annotation examples to match a route based on the host There's no any example with PHP annotations currently. --- routing/hostname_pattern.rst | 142 ++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/routing/hostname_pattern.rst b/routing/hostname_pattern.rst index ce59fd6cb0d..27de3228387 100644 --- a/routing/hostname_pattern.rst +++ b/routing/hostname_pattern.rst @@ -8,6 +8,33 @@ You can also match on the HTTP *host* of the incoming request. .. configuration-block:: + .. code-block:: php-annotations + + // src/Acme/DemoBundle/Controller/MainController.php + namespace Acme\DemoBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + + class MainController extends Controller + { + /** + * @Route("/", name="mobile_homepage", host="m.example.com") + */ + public function mobileHomepageAction() + { + // ... + } + + /** + * @Route("/", name="homepage") + */ + public function homepageAction() + { + // ... + } + } + .. code-block:: yaml mobile_homepage: @@ -63,12 +90,39 @@ you can use placeholders in your hostname: .. configuration-block:: + .. code-block:: php-annotations + + // src/Acme/DemoBundle/Controller/MainController.php + namespace Acme\DemoBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + + class MainController extends Controller + { + /** + * @Route("/", name="projects_homepage", host="{project_name}.example.com") + */ + public function projectsHomepageAction() + { + // ... + } + + /** + * @Route("/", name="homepage") + */ + public function homepageAction() + { + // ... + } + } + .. code-block:: yaml projects_homepage: path: / host: "{project_name}.example.com" - defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } + defaults: { _controller: AcmeDemoBundle:Main:projectsHomepage } homepage: path: / @@ -83,7 +137,7 @@ you can use placeholders in your hostname: http://symfony.com/schema/routing/routing-1.0.xsd"> - AcmeDemoBundle:Main:mobileHomepage + AcmeDemoBundle:Main:projectsHomepage @@ -98,7 +152,7 @@ you can use placeholders in your hostname: $collection = new RouteCollection(); $collection->add('project_homepage', new Route('/', array( - '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', + '_controller' => 'AcmeDemoBundle:Main:projectsHomepage', ), array(), array(), '{project_name}.example.com')); $collection->add('homepage', new Route('/', array( @@ -113,6 +167,39 @@ instance, if you want to match both ``m.example.com`` and .. configuration-block:: + .. code-block:: php-annotations + + // src/Acme/DemoBundle/Controller/MainController.php + namespace Acme\DemoBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + + class MainController extends Controller + { + /** + * @Route( + * "/", + * name="mobile_homepage", + * host="{subdomain}.example.com", + * defaults={"subdomain"="m"}, + * requirements={"subdomain"="m|mobile"} + * ) + */ + public function mobileHomepageAction() + { + // ... + } + + /** + * @Route("/", name="homepage") + */ + public function homepageAction() + { + // ... + } + } + .. code-block:: yaml mobile_homepage: @@ -173,6 +260,39 @@ instance, if you want to match both ``m.example.com`` and .. configuration-block:: + .. code-block:: php-annotations + + // src/Acme/DemoBundle/Controller/MainController.php + namespace Acme\DemoBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + + class MainController extends Controller + { + /** + * @Route( + * "/", + * name="mobile_homepage", + * host="m.{domain}", + * defaults={"domain"="%domain%"}, + * requirements={"domain"="%domain%"} + * ) + */ + public function mobileHomepageAction() + { + // ... + } + + /** + * @Route("/", name="homepage") + */ + public function homepageAction() + { + // ... + } + } + .. code-block:: yaml mobile_homepage: @@ -241,6 +361,22 @@ You can also set the host option on imported routes: .. configuration-block:: + .. code-block:: php-annotations + + // src/Acme/HelloBundle/Controller/MainController.php + namespace Acme\HelloBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + + /** + * @Route(host="hello.example.com") + */ + class MainController extends Controller + { + // ... + } + .. code-block:: yaml acme_hello: