Commit 1cf18343 authored by Fabien Potencier's avatar Fabien Potencier

merged igorw/https

parents 9fa8f9ba 09968780
...@@ -196,6 +196,9 @@ of them. ...@@ -196,6 +196,9 @@ of them.
$id = $app['request']->get('id'); $id = $app['request']->get('id');
This is only available when a request is being served, you can only access it
from within a controller, before filter, after filter or error handler.
* **autoloader**: This service provides you with a * **autoloader**: This service provides you with a
`UniversalClassLoader `UniversalClassLoader
<http://api.symfony.com/2.0/Symfony/Component/ClassLoader/UniversalClassLoader.html>`_ <http://api.symfony.com/2.0/Symfony/Component/ClassLoader/UniversalClassLoader.html>`_
...@@ -234,6 +237,28 @@ of them. ...@@ -234,6 +237,28 @@ of them.
Symfony2, it takes a Request as input and returns a Symfony2, it takes a Request as input and returns a
Response as output. Response as output.
* **request_context**: The request context is a simplified representation
of the request that is used by the Router and the UrlGenerator.
.. note:: .. note::
All of these Silex core services are shared. All of these Silex core services are shared.
Core parameters
---------------
* **request.http_port** (optional): Allows you to override the default port
for non-HTTPS URLs. If the current request is HTTP, it will always use the
current port.
Defaults to 80.
This parameter can be used by the ``UrlGeneratorExtension``.
* **request.https_port** (optional): Allows you to override the default port
for HTTPS URLs. If the current request is HTTPS, it will always use the
current port.
Defaults to 443.
This parameter can be used by the ``UrlGeneratorExtension``.
...@@ -81,6 +81,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -81,6 +81,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this['kernel'] = $this->share(function () use ($app) { $this['kernel'] = $this->share(function () use ($app) {
return new HttpKernel($app['dispatcher'], $app['resolver']); return new HttpKernel($app['dispatcher'], $app['resolver']);
}); });
$this['request.http_port'] = 80;
$this['request.https_port'] = 443;
} }
/** /**
...@@ -313,12 +316,14 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -313,12 +316,14 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
public function onCoreRequest(KernelEvent $event) public function onCoreRequest(KernelEvent $event)
{ {
$this['request'] = $event->getRequest(); $this['request'] = $event->getRequest();
$this['request_context'] = new RequestContext( $this['request_context'] = new RequestContext(
$this['request']->getBaseUrl(), $this['request']->getBaseUrl(),
$this['request']->getMethod(), $this['request']->getMethod(),
$this['request']->getHost(), $this['request']->getHost(),
$this['request']->getPort(), $this['request']->getScheme(),
$this['request']->getScheme() !$this['request']->isSecure() ? $this['request']->getPort() : $this['request.http_port'],
$this['request']->isSecure() ? $this['request']->getPort() : $this['request.https_port']
); );
$this['controllers']->flush(); $this['controllers']->flush();
......
...@@ -110,6 +110,26 @@ class Controller ...@@ -110,6 +110,26 @@ class Controller
return $this; return $this;
} }
/**
* Sets the requirement of HTTP (no HTTPS) on this controller.
*/
public function requireHttp()
{
$this->route->setRequirement('_scheme', 'http');
return $this;
}
/**
* Sets the requirement of HTTPS on this controller.
*/
public function requireHttps()
{
$this->route->setRequirement('_scheme', 'https');
return $this;
}
/** /**
* Freezes the controller. * Freezes the controller.
* *
......
...@@ -29,6 +29,23 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -29,6 +29,23 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
$app->register(new UrlGeneratorExtension()); $app->register(new UrlGeneratorExtension());
$app->get('/hello/{name}', function ($name) {})
->bind('hello');
$app->get('/', function () {});
$request = Request::create('/');
$app->handle($request);
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGenerator', $app['url_generator']);
}
public function testUrlGeneration()
{
$app = new Application();
$app->register(new UrlGeneratorExtension());
$app->get('/hello/{name}', function ($name) {}) $app->get('/hello/{name}', function ($name) {})
->bind('hello'); ->bind('hello');
...@@ -38,6 +55,66 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -38,6 +55,66 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/'); $request = Request::create('/');
$response = $app->handle($request); $response = $app->handle($request);
$this->assertEquals('/hello/john', $response->getContent()); $this->assertEquals('/hello/john', $response->getContent());
} }
public function testAbsoluteUrlGeneration()
{
$app = new Application();
$app->register(new UrlGeneratorExtension());
$app->get('/hello/{name}', function ($name) {})
->bind('hello');
$app->get('/', function () use ($app) {
return $app['url_generator']->generate('hello', array('name' => 'john'), true);
});
$request = Request::create('https://localhost:81/');
$response = $app->handle($request);
$this->assertEquals('https://localhost:81/hello/john', $response->getContent());
}
public function testUrlGenerationWithHttp()
{
$app = new Application();
$app->register(new UrlGeneratorExtension());
$app->get('/insecure', function () {})
->bind('insecure_page')
->requireHttp();
$app->get('/', function () use ($app) {
return $app['url_generator']->generate('insecure_page');
});
$request = Request::create('https://localhost/');
$response = $app->handle($request);
$this->assertEquals('http://localhost/insecure', $response->getContent());
}
public function testUrlGenerationWithHttps()
{
$app = new Application();
$app->register(new UrlGeneratorExtension());
$app->get('/secure', function () {})
->bind('secure_page')
->requireHttps();
$app->get('/', function () use ($app) {
return $app['url_generator']->generate('secure_page');
});
$request = Request::create('http://localhost/');
$response = $app->handle($request);
$this->assertEquals('https://localhost/secure', $response->getContent());
}
} }
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