aboutsummaryrefslogtreecommitdiff
path: root/app/class/Controller.php
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2020-04-20 23:58:41 +0200
committerVincent Peugnet <33429034+vincent-peugnet@users.noreply.github.com>2020-04-21 14:56:53 +0200
commit648c9a70684bba41ec9ba4890cafdc6950a407dd (patch)
treee61bddd05f928a384de2d41938a448d6bdb6c436 /app/class/Controller.php
parent7c27d746253a446483182e9bcec9d565c53172c4 (diff)
downloadwcms-648c9a70684bba41ec9ba4890cafdc6950a407dd.tar.gz
wcms-648c9a70684bba41ec9ba4890cafdc6950a407dd.zip
refactor: error handling uncatched LogicException ex
Diffstat (limited to 'app/class/Controller.php')
-rw-r--r--app/class/Controller.php31
1 files changed, 24 insertions, 7 deletions
diff --git a/app/class/Controller.php b/app/class/Controller.php
index 3133ecb..4185be8 100644
--- a/app/class/Controller.php
+++ b/app/class/Controller.php
@@ -4,6 +4,8 @@ namespace Wcms;
use DateTime;
use DateTimeImmutable;
+use Exception;
+use InvalidArgumentException;
use League\Plates\Engine;
class Controller
@@ -44,11 +46,11 @@ class Controller
{
$router = $this->router;
$this->plates = new Engine(Model::TEMPLATES_DIR);
- $this->plates->registerFunction('url', function (string $string, array $vars = []) use ($router) {
- return $router->generate($string, $vars);
+ $this->plates->registerFunction('url', function (string $string, array $vars = []) {
+ return $this->generate($string, $vars);
});
- $this->plates->registerFunction('upage', function (string $string, string $id) use ($router) {
- return $router->generate($string, ['page' => $id]);
+ $this->plates->registerFunction('upage', function (string $string, string $id) {
+ return $this->generate($string, ['page' => $id]);
});
$this->plates->addData(['flashmessages' => Model::getflashmessages()]);
}
@@ -72,7 +74,22 @@ class Controller
-
+ /**
+ * Generate the URL for a named route. Replace regexes with supplied parameters.
+ *
+ * @param string $route The name of the route.
+ * @param array $params Associative array of parameters to replace placeholders with.
+ * @return string The URL of the route with named parameters in place.
+ * @throws InvalidArgumentException If the route does not exist.
+ */
+ public function generate(string $route, array $params = []): string
+ {
+ try {
+ return $this->router->generate($route, $params);
+ } catch (Exception $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ }
public function redirect($url)
{
@@ -81,7 +98,7 @@ class Controller
public function routedirect(string $route, array $vars = [])
{
- $this->redirect($this->router->generate($route, $vars));
+ $this->redirect($this->generate($route, $vars));
}
public function routedirectget(string $route, array $vars = [])
@@ -91,7 +108,7 @@ class Controller
$get .= $key . '=' . $value . '&';
}
$get = rtrim($get, '&');
- $this->redirect($this->router->generate($route, []) . $get);
+ $this->redirect($this->generate($route, []) . $get);
}
public function error(int $code)