Commit 0c3b670b authored by Fabien Potencier's avatar Fabien Potencier

merged branch tiraeth/host (PR #648)

This PR was squashed before being merged into the master branch (closes #648).

Discussion
----------

Added support for URL host from Routing Component

Hi everyone,

Firstly, I wanted to thank you all for Silex. It's fabulous.

I needed URL host support in Silex to manage different subdomains of my small application and came up with an idea of introducing `->host($host)` chained method to `Silex\Route` class. This way users can apply host resolution to controllers in Silex.

This PR includes above functionality + one unit test to check if everything works as expected.

URL host support is of course available in Routing Component from version `>=2.2`, but Silex relies on 2.2 anyway.

Example:
```php
<?php

// ...

$app->match('/', function() {
    // app-specific action
})->host('example.com');

$app->match('/', function ($user) {
    // user-specific action
})->host('{user}.example.com');

// ...
```

I am open for comments.

Commits
-------

f0124c7f Added support for URL host from Routing Component
parents 3b0e0349 f0124c7f
......@@ -86,6 +86,20 @@ class Route extends BaseRoute
return $this;
}
/**
* Sets the requirement of host on this Route
*
* @param string $host The host for which this route should be enabled
*
* @return Route $this The current Route instance
*/
public function host($host)
{
$this->setHost($host);
return $this;
}
/**
* Sets the requirement of HTTP (no HTTPS) on this Route.
*
......
......@@ -179,6 +179,14 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/foo/', $response->getTargetUrl());
}
public function testHostSpecification()
{
$route = new \Silex\Route();
$this->assertSame($route, $route->host('{locale}.example.com'));
$this->assertEquals('{locale}.example.com', $route->getHost());
}
public function testRequireHttpRedirect()
{
$app = new Application();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment