Witam, napisałem kod Front controllera oraz Routera i chciałbym jeszcze do tego dodać tablice routingu. Znalazłem na GitHubie (
https://github.com/simonhamp/routes) rozwiązanie, które chciałbym dodać do mojego kodu ale kompletnie mi to nie wychodzi.
Front Controller
<?php
require_once SYS_PATH . 'core/controller.php';
require_once SYS_PATH . 'core/router.php';
class Bootstrap
{
private $controller;
private $action;
public function init(Router $request)
{
$controller = $request->getController();
$method = $request->getMethod();
$args = $request->getArgs();
$controllerFile = APP_PATH. 'controllers/'.$controller.'.php';
require_once $controllerFile;
$controller = new $controller;
$method = (is_callable
(array($controller,$method))) ?
$method : 'index';
call_user_func_array
(array($controller,$method),$args); }else{
call_user_func
(array($controller,$method)); }
return;
}
}
}
Router
<?php
class Router
{
private $controller;
private $action;
private $args;
public function __construct()
{
$path_info = !empty($_SERVER['PATH_INFO']) ?
$_SERVER['PATH_INFO'] : (!empty($_SERVER['ORIG_PATH_INFO']) ?
$_SERVER['ORIG_PATH_INFO'] : '');
$this->controller = ($c = array_shift($parts)) ?
$c : 'welcome'; $this->action = ($c = array_shift($parts)) ?
$c : 'index'; $this->args = (isset($parts[0
])) ?
$parts : array(); }
public function getController()
{
return $this->controller;
}
public function getMethod()
{
return $this->action;
}
public function getArgs()
{
return $this->args;
}
}