Adds phpdoc comments
parent
c711d6f011
commit
2bd6cf89f6
|
@ -1,13 +1,37 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Router
|
||||||
|
*
|
||||||
|
* A simple Router class to manage URL-to-callback mapping and dispatch requests to the appropriate controllers and methods.
|
||||||
|
* The class supports defining routes, matching URLs against patterns, and invoking callbacks for matched routes.
|
||||||
|
*/
|
||||||
class Router {
|
class Router {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array $routes Associative array of route patterns and their corresponding callbacks.
|
||||||
|
*/
|
||||||
private $routes = [];
|
private $routes = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new route to the router.
|
||||||
|
*
|
||||||
|
* @param string $pattern The URL pattern to match (regular expression).
|
||||||
|
* @param string $callback The callback for the route in the format "Controller@Method".
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public function add() {
|
public function add() {
|
||||||
$this->routes[$pattern] = $callback;
|
$this->routes[$pattern] = $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispatches a request to the appropriate route callback.
|
||||||
|
*
|
||||||
|
* @param string $url The URL to match against the defined routes.
|
||||||
|
*
|
||||||
|
* @return void Outputs the result of the invoked callback or a 404 error if no route matches.
|
||||||
|
*/
|
||||||
public function dispatch($url) {
|
public function dispatch($url) {
|
||||||
// remove variables from url
|
// remove variables from url
|
||||||
$url = strtok($url, '?');
|
$url = strtok($url, '?');
|
||||||
|
@ -25,9 +49,18 @@ class Router {
|
||||||
echo '404 page not found';
|
echo '404 page not found';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invokes the callback for a matched route.
|
||||||
|
*
|
||||||
|
* @param string $callback The callback for the route in the format "Controller@Method".
|
||||||
|
* @param array $params Parameters extracted from the route pattern.
|
||||||
|
*
|
||||||
|
* @return void Executes the specified method on the specified controller with the provided parameters.
|
||||||
|
*
|
||||||
|
* @throws Exception If the controller class or method does not exist.
|
||||||
|
*/
|
||||||
private function invoke($callback, $params) {
|
private function invoke($callback, $params) {
|
||||||
list($controllerName, $methodName) = explode('@', $callback);
|
list($controllerName, $methodName) = explode('@', $callback);
|
||||||
// $controllerClass = "\\App\\Controllers\\$controllerName";
|
|
||||||
$controllerClass = "../pages/$pageName";
|
$controllerClass = "../pages/$pageName";
|
||||||
$controller = new $controllerClass();
|
$controller = new $controllerClass();
|
||||||
call_user_func_array([$controller, $methodName], $params);
|
call_user_func_array([$controller, $methodName], $params);
|
||||||
|
|
Loading…
Reference in New Issue