Bonjour,
j'ai suivi le tuto pour creer un routeur sur ce site, je l'ai ajouté au model mvc vue ici egalement.
Tout fonctionne bien ou presque, lorsque je mets une url ça me renvoi bien sur le bon controller et sur la bonne methode. Cependant mon problème est que les liens vers mes fichiers css passe dans le routeur et donc renvoi sur un notfound(ce qui est logique).
Donc je ne sais pas comment éviter que mes liens vers mes fichiers soient pris en compte par ce système.
class router
<?php
namespace Core\Router;
class Router
{
private $url;
private $routes = [];
private $namedRoutes = [];
public function __construct($url)
{
$this->url = $url;
}
public function get($path, $callable, $name = null)
{
return $this->add($path, $callable, $name, 'GET');
}
public function post($path, $callable, $name = null)
{
return $this->add($path, $callable, $name, 'POST');
}
public function add($path, $callable, $name, $method)
{
$route = new Route($path, $callable);
$this->routes[$method][] = $route;
if($name){
$this->namedRoutes[$name] = $route;
}
return $route;
}
public function run()
{
if(!isset($this->routes[$_SERVER['REQUEST_METHOD']])){
throw new RouterException('REQUEST_METHOD does not exist');
}
foreach($this->routes[$_SERVER['REQUEST_METHOD']] as $route){
if($route->match($this->url)){
return $route->call();
}
}
throw new RouterException('No matching routes');
header('Location: '.WEBROOT.'notfound');
}
public function url($name, $params = [])
{
if(!isset($this->namedRoutes[$name])){
throw new RouterException('No route matches this name');
}
return $this->namedRoutes[$name]->getUrl($params);
}
}
class route
<?php
namespace Core\Router;
class Route
{
private $path;
private $callable;
private $matches = [];
private $params = [];
public function __construct($path, $callable)
{
$this->path = trim($path, '/');
$this->callable = $callable;
}
public function match($url)
{
$url = trim($url, '/');
$path = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], $this->path);
$regex = "#^$path$#i";
if(!preg_match($regex, $url, $matches)){
return false;
}
array_shift($matches);
$this->matches = $matches;
return true;
}
private function paramMatch($match)
{
if(isset($this->params[$match[1]])){
return '('.$this->params[$match[1]].')';
}
return '([^/]+)';
}
public function call()
{
if(is_string($this->callable)){
$page = explode('#', $this->callable);
if($page[0] === 'admin'){
$controller = '\\App\\Controller\\Admin\\'.ucfirst($page[1]).'Controller';
$action = $page[2];
}else{
$controller = '\\App\\Controller\\'.ucfirst($page[0]).'Controller';
$action = $page[1];
}
$controle = str_replace('\\', '/', $controller).'.php';
if(!file_exists(ROOT.$controle)){
header('Location: '.WEBROOT.'notfound');
}
$controller = new $controller();
return call_user_func_array([$controller, $action], $this->matches);
}else{
return call_user_func_array($this->callable, $this->matches);
}
}
public function with($param, $regex)
{
$this->params[$param] = str_replace('(', '(?:', $regex);
return $this;
}
public function getUrl($params)
{
$path = $this->path;
foreach($params as $k => $v){
$path = str_replace(":$k", $v, $path);
}
return $path;
}
}