Skip to content

Add annotation examples to match a route based on the host #7839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 139 additions & 3 deletions routing/hostname_pattern.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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: /
Expand All @@ -83,7 +137,7 @@ you can use placeholders in your hostname:
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>
<default key="_controller">AcmeDemoBundle:Main:projectsHomepage</default>
</route>

<route id="homepage" path="/">
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down