Commit e310b20a authored by Igor Wiedler's avatar Igor Wiedler

allow string responses which are converted to Response objects

parent a98de661
...@@ -5,17 +5,16 @@ Silex is a simple web framework to develop simple websites: ...@@ -5,17 +5,16 @@ Silex is a simple web framework to develop simple websites:
require_once __DIR__.'/silex.phar'; require_once __DIR__.'/silex.phar';
use Symfony\Component\HttpFoundation\Response;
use Silex\Framework; use Silex\Framework;
$framework = new Framework(array( $framework = new Framework(array(
'/hello/:name' => function($name) '/hello/:name' => function($name)
{ {
return new Response('Hello '.$name); return "Hello $name";
}, },
'GET|PUT|POST /goodbye/:name' => function($name) 'GET|PUT|POST /goodbye/:name' => function($name)
{ {
return new Response('Goodbye '.$name); return "Goodbye $name";
}, },
)); ));
......
...@@ -3,17 +3,16 @@ ...@@ -3,17 +3,16 @@
require_once __DIR__.'/silex.phar'; require_once __DIR__.'/silex.phar';
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Silex\Framework; use Silex\Framework;
$hello = function ($name) $hello = function($name)
{ {
return new Response('Hello '.$name); return "Hello $name";
}; };
$goodbye = function($name) $goodbye = function($name)
{ {
return new Response('Goodbye '.$name); return "Goodbye $name";
}; };
$framework = new Framework(array( $framework = new Framework(array(
......
...@@ -5,6 +5,7 @@ namespace Silex; ...@@ -5,6 +5,7 @@ namespace Silex;
use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Controller\ControllerResolver; use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
...@@ -46,6 +47,7 @@ class Framework extends HttpKernel ...@@ -46,6 +47,7 @@ class Framework extends HttpKernel
$dispatcher = new EventDispatcher(); $dispatcher = new EventDispatcher();
$dispatcher->connect('core.request', array($this, 'parseRequest')); $dispatcher->connect('core.request', array($this, 'parseRequest'));
$dispatcher->connect('core.view', array($this, 'parseResponse'));
$resolver = new ControllerResolver(); $resolver = new ControllerResolver();
parent::__construct($dispatcher, $resolver); parent::__construct($dispatcher, $resolver);
...@@ -77,4 +79,15 @@ class Framework extends HttpKernel ...@@ -77,4 +79,15 @@ class Framework extends HttpKernel
$request->attributes->add($attributes); $request->attributes->add($attributes);
} }
public function parseResponse(Event $event, $response)
{
// convert return value into a response object
if (!$response instanceof Response)
{
return new Response((string) $response);
}
return $response;
}
} }
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