summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2016-11-29 16:53:49 +0100
committerGitHub <noreply@github.com>2016-11-29 16:53:49 +0100
commit8db24c7d9e173bb8a126dfde4889f3316d892d66 (patch)
treeab215c24bea919013537d67a193ab27f73309c79 /src
parent85b270a15e378d2e1862d0e797b9ec44a6925bed (diff)
parent8f42e1e3a34b1b7eab9892a9d4a40efe524eba42 (diff)
downloadrouter-8db24c7d9e173bb8a126dfde4889f3316d892d66.zip
router-8db24c7d9e173bb8a126dfde4889f3316d892d66.tar.gz
router-8db24c7d9e173bb8a126dfde4889f3316d892d66.tar.bz2
Merge pull request #15 from jasny/controller-runner-notfound
Let the controller runner return a not found response
Diffstat (limited to 'src')
-rw-r--r--src/Router/Runner/Controller.php25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/Router/Runner/Controller.php b/src/Router/Runner/Controller.php
index 7985029..3d5e569 100644
--- a/src/Router/Runner/Controller.php
+++ b/src/Router/Runner/Controller.php
@@ -12,6 +12,25 @@ use Psr\Http\Message\ResponseInterface;
class Controller extends Runner
{
/**
+ * Return with a 404 not found response
+ *
+ * @param ServerRequestInterface $request
+ * @param ResponseInterface $response
+ * @return ResponseInterface
+ */
+ protected function notFound(ServerRequestInterface $request, ResponseInterface $response)
+ {
+ $finalResponse = $response
+ ->withProtocolVersion($request->getProtocolVersion())
+ ->withStatus(404)
+ ->withHeader('Content-Type', 'text/plain');
+
+ $finalResponse->getBody()->write("Not found");
+
+ return $finalResponse;
+ }
+
+ /**
* Get class name from controller name
*
* @param string $name
@@ -49,11 +68,13 @@ class Controller extends Runner
$class = $this->getClass($name);
if (!class_exists($class)) {
- throw new \RuntimeException("Can not route to controller '$class': class not exists");
+ trigger_error("Can't route to controller '$class': class not exists", E_USER_NOTICE);
+ return $this->notFound($request, $response);
}
if (!method_exists($class, '__invoke')) {
- throw new \RuntimeException("Can not route to controller '$class': class does not have '__invoke' method");
+ trigger_error("Can't route to controller '$class': class does not have '__invoke' method", E_USER_NOTICE);
+ return $this->notFound($request, $response);
}
$controller = $this->instantiate($class);