report 405s correctly

This commit is contained in:
Alex Grintsvayg 2016-09-06 09:18:25 -04:00
parent 4f662ba9e1
commit dcd3e22ad4
5 changed files with 31 additions and 3 deletions

View file

@ -60,6 +60,12 @@ class Controller
{
return NavActions::execute404();
}
catch (\Routing\HttpMethodNotAllowedException $e)
{
Response::setStatus(405);
Response::setHeader('Allow', implode(', ', $e->getAllowedMethods()));
return ['page/404'];
}
}
protected static function getRouterWithRoutes(): \Routing\RouteCollector

View file

@ -93,6 +93,7 @@ news:
next: Next
prev: Previous
page:
badmethod: HTTP method not allowed
faq:
back: Back to FAQ
header: Frequently Asked Questions

View file

@ -175,7 +175,7 @@ class Dispatcher
$this->matchedRoute = $routes;
throw new HttpMethodNotAllowedException('Allow: ' . implode(', ', array_keys($routes)));
throw new HttpMethodNotAllowedException(array_keys($routes), 'Method not allowed');
}
/**

View file

@ -2,4 +2,18 @@
namespace Routing;
class HttpMethodNotAllowedException extends HttpException {}
class HttpMethodNotAllowedException extends HttpException
{
protected $allowedMethods;
public function __construct(array $allowedMethods, $message, $code, Exception $previous)
{
$this->allowedMethods = $allowedMethods;
parent::__construct($message, $code, $previous);
}
public function getAllowedMethods()
{
return $this->allowedMethods;
}
}

View file

@ -0,0 +1,7 @@
<?php echo View::render('nav/_header', ['isDark' => false]) ?>
<main>
<div class="content">
<h1>{{page.badmethod}}</h1>
<p>{{page.funnier}}</p>
</div>
</main>