Browse Source

Router: Combine addRoute() and addBasicRoute() functionality

master
Riyyi 2 years ago
parent
commit
54eee2416a
  1. 104
      app/classes/Router.php
  2. 24
      route.php

104
app/classes/Router.php

@ -41,7 +41,7 @@ class Router {
// Process basic routes // Process basic routes
foreach (self::$routes as $route) { foreach (self::$routes as $route) {
// ["/example/my-page", "ExampleController", "action", "" : ["view", "title", "description"]], // ["/example/my-page", "ExampleController", "action", "" : ["view", "title", "description"]],
self::addBasicRoute(['GET', 'POST'], $route); self::addRoute(['GET', 'POST'], $route);
} }
self::createNavigation(); self::createNavigation();
@ -74,13 +74,13 @@ class Router {
* DELETE /route/{id} destroyAction * DELETE /route/{id} destroyAction
*/ */
self::addRoute(['GET'], [$route, $controller, 'indexAction']); self::addRoute(['GET'], [$route, $controller, 'index']);
self::addRoute(['GET'], [$route . '/create', $controller, 'createAction']); self::addRoute(['GET'], [$route . '/create', $controller, 'create']);
self::addRoute(['POST'], [$route, $controller, 'storeAction']); self::addRoute(['POST'], [$route, $controller, 'store']);
self::addRoute(['GET'], [$route . '/[i:id]', $controller, 'showAction', ['id']]); self::addRoute(['GET'], [$route . '/[i:id]', $controller, 'show']);
self::addRoute(['GET'], [$route . '/[i:id]/edit', $controller, 'editAction', ['id']]); self::addRoute(['GET'], [$route . '/[i:id]/edit', $controller, 'edit']);
self::addRoute(['PUT', 'PATCH'], [$route . '/[i:id]', $controller, 'updateAction', ['id']]); self::addRoute(['PUT', 'PATCH'], [$route . '/[i:id]', $controller, 'update']);
self::addRoute(['DELETE'], [$route . '/[i:id]', $controller, 'destroyAction', ['id']]); self::addRoute(['DELETE'], [$route . '/[i:id]', $controller, 'destroy']);
} }
//-------------------------------------// //-------------------------------------//
@ -139,7 +139,7 @@ class Router {
$url = '/' . $section[$page['section_id']] . '/' . $page['page']; $url = '/' . $section[$page['section_id']] . '/' . $page['page'];
// Add route // Add route
self::$routes[] = [$url, 'PageController', 'route', $page['id']]; self::$routes[] = [$url, 'PageController', 'route', [$page['id']]];
} }
// Cache sections and pages // Cache sections and pages
@ -149,94 +149,53 @@ class Router {
protected static function addRoute(array $method = [], array $data = []): void protected static function addRoute(array $method = [], array $data = []): void
{ {
if (!_exists($method) || !_exists($data)) { if (!_exists($method) || !_exists($data) || count($data) < 2) {
return; return;
} }
$route = $data[0] ?? ''; $route = $data[0] ?? null;
$controller = $data[1] ?? ''; $controller = $data[1] ?? null;
$action = $data[2] ?? ''; $action = $data[2] ?? null;
$param = $data[3] ?? []; $parameters = $data[3] ?? [];
if ($route == '' || $controller == '' || $action == '') {
return;
}
// Create Klein route
self::$router->respond($method, $route, function($request, $response, $service)
use($controller, $action, $param) {
// Create new Controller object
$controller = '\App\Controllers\\' . $controller;
$controller = new $controller(self::$router);
// If method does not exist in object
if (!method_exists($controller, $action)) {
return $controller->throw404();
}
// If no valid permissions
if ($controller->getAdminSection() &&
$controller->getLoggedIn() == false) {
return $controller->throw404();
}
// Loop through params if (!$route || !$controller || !is_array($parameters)) {
$params = [];
foreach ($param as $name) {
$params[] = $request->param($name);
}
// Call Controller action
return $controller->{$action}(...$params);
});
}
protected static function addBasicRoute(array $method = [], array $route = []): void
{
if (!_exists($method) || !_exists($route)) {
return; return;
} }
// Create Klein route // Create Klein route
self::$router->respond($method, $route[0], function() use($route) { self::$router->respond($method, $route, function($request, $response, $service) use($controller, $action, $parameters) {
// Create new Controller object // Create new Controller object
$controller = '\App\Controllers\\' . $route[1]; $controller = '\App\Controllers\\' . $controller;
$controller = new $controller(self::$router); $controller = new $controller(self::$router);
// Complete action variable // Complete action variable
if ($route[2] == '') { if (!$action) {
$route[2] = 'indexAction'; $action = 'indexAction';
} }
else { else {
$route[2] .= 'Action'; $action .= 'Action';
} }
// If method does not exist in object // If method does not exist in object
if (!method_exists($controller, $route[2])) { if (!method_exists($controller, $action)) {
return $controller->throw404(); return $controller->throw404();
} }
// If no valid permissions // If no valid permissions
if ($controller->getAdminSection() && if ($controller->getAdminSection() && !$controller->getLoggedIn()) {
$controller->getLoggedIn() == false) {
return $controller->throw404(); return $controller->throw404();
} }
// Get URL parameters
$namedParameters = array_filter($request->paramsNamed()->all(), 'is_string', ARRAY_FILTER_USE_KEY);
$namedParameters = array_values($namedParameters);
// Append URL parameters to the hard-coded parameters
array_push($parameters, ...$namedParameters);
// Call Controller action // Call Controller action
if (is_array($route[3])) { return $controller->{$action}(...$parameters);
return $controller->{$route[2]}(
$route[3][0] ?? '',
$route[3][1] ?? '',
$route[3][2] ?? ''
);
}
else if ($route[3] != '') {
return $controller->{$route[2]}($route[3]);
}
else {
return $controller->{$route[2]}();
}
}); });
} }
@ -326,6 +285,3 @@ class Router {
} }
Router::_init(); Router::_init();
// @Todo
// - combine addRoute and addBasicroute functionality

24
route.php

@ -12,20 +12,20 @@ Router::resource('/admin/media', 'MediaController');
// Basic routes // Basic routes
return [ return [
// URL, controller, action, view/title/description // URL, controller, action, [view, title, description]
['/', 'IndexController', '', ''], ['/', 'IndexController'],
['/img/captcha.jpg', 'IndexController', 'captcha', ''], ['/img/captcha.jpg', 'IndexController', 'captcha'],
['/robots.txt', 'IndexController', 'robots', ''], ['/robots.txt', 'IndexController', 'robots'],
['/sitemap.xml', 'IndexController', 'sitemap', ''], ['/sitemap.xml', 'IndexController', 'sitemap'],
['/login', 'LoginController', 'login', ['', 'Sign in', '']], ['/login', 'LoginController', 'login', ['', 'Sign in', '']],
['/reset-password', 'LoginController', 'reset', ['', 'Reset password', '']], ['/reset-password', 'LoginController', 'reset', ['', 'Reset password', '']],
['/logout', 'LoginController', 'logout', ''], ['/logout', 'LoginController', 'logout'],
['/admin', 'AdminController', '', ''], ['/admin', 'AdminController'],
['/admin/cache', 'CacheController', 'cache', ''], ['/admin/cache', 'CacheController', 'cache'],
['/admin/toggle', 'AdminController', 'toggle', ''], ['/admin/cache/purge', 'CacheController', 'purge'],
['/admin/cache/purge', 'CacheController', 'purge', ''], ['/admin/cache/toggle', 'CacheController', 'toggle'],
['/admin/cache/toggle', 'CacheController', 'toggle', ''], ['/admin/toggle', 'AdminController', 'toggle'],
['/admin/syntax-highlighting', 'AdminController', 'syntax', ''], ['/admin/syntax-highlighting', 'AdminController', 'syntax'],
['/test', 'TestController', '', ''], ['/test', 'TestController', '', ''],
// ["", "", "", ""], // ["", "", "", ""],
]; ];

Loading…
Cancel
Save