Commit 87a7a0ec authored by Fabien Potencier's avatar Fabien Potencier

added Application::abort() to aborts a request early

That simplifies firing 404 pages for instance as you don't need
to add a use statement for the HttpException.
parent 8659ca11
...@@ -122,11 +122,9 @@ Dynamic routing ...@@ -122,11 +122,9 @@ Dynamic routing
Now, you can create another controller for viewing individual blog Now, you can create another controller for viewing individual blog
posts:: posts::
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; $app->get('/blog/show/{id}', function ($id) use ($app, $blogPosts) {
$app->get('/blog/show/{id}', function ($id) use ($blogPosts) {
if (!isset($blogPosts[$id])) { if (!isset($blogPosts[$id])) {
throw new NotFoundHttpException(); $app->abort(404, "Post $id does not exist.");
} }
$post = $blogPosts[$id]; $post = $blogPosts[$id];
...@@ -407,6 +405,17 @@ once a response is returned, the following handlers are ignored. ...@@ -407,6 +405,17 @@ once a response is returned, the following handlers are ignored.
// logic to handle the error and return a Response // logic to handle the error and return a Response
}); });
The error handlers are also called when you use ``abort`` to abort a request
early::
$app->get('/blog/show/{id}', function ($id) use ($app, $blogPosts) {
if (!isset($blogPosts[$id])) {
$app->abort(404, "Post $id does not exist.");
}
return new Response(...);
});
Redirects Redirects
--------- ---------
......
...@@ -21,6 +21,7 @@ use Symfony\Component\HttpKernel\Event\FilterControllerEvent; ...@@ -21,6 +21,7 @@ use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
...@@ -212,6 +213,17 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -212,6 +213,17 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this['dispatcher']->addListener(SilexEvents::AFTER, $callback); $this['dispatcher']->addListener(SilexEvents::AFTER, $callback);
} }
/**
* Aborts the current request by sending a proper HTTP error.
*
* @param integer $statusCode The HTTP status code
* @param array $headers An array of HTTP headers
*/
public function abort($statusCode, $message = '', array $headers = array())
{
throw new HttpException($statusCode, $message, null, $headers);
}
/** /**
* Registers an error handler. * Registers an error handler.
* *
......
...@@ -14,8 +14,6 @@ namespace Silex\Extension; ...@@ -14,8 +14,6 @@ namespace Silex\Extension;
use Silex\Application; use Silex\Application;
use Silex\ExtensionInterface; use Silex\ExtensionInterface;
use Symfony\Component\Routing\Generator\UrlGenerator;
class SymfonyBridgesExtension implements ExtensionInterface class SymfonyBridgesExtension implements ExtensionInterface
{ {
public function register(Application $app) public function register(Application $app)
......
...@@ -14,6 +14,7 @@ namespace Silex\Tests; ...@@ -14,6 +14,7 @@ namespace Silex\Tests;
use Silex\Application; use Silex\Application;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
/** /**
* Application test cases. * Application test cases.
...@@ -104,6 +105,18 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -104,6 +105,18 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foobar', $response->getContent()); $this->assertEquals('foobar', $response->getContent());
} }
public function testAbort()
{
$app = new Application();
try {
$app->abort(404);
$this->fail();
} catch (HttpException $e) {
$this->assertEquals(404, $e->getStatusCode());
}
}
/** /**
* @dataProvider escapeProvider * @dataProvider escapeProvider
*/ */
......
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