Bonjour,
a la minute 39 et 46 seconds a ce tutoriel https://www.youtube.com/watch?v=I-DN2C7Gs7A
j'ai un message d'erreur
Fatal error: Uncaught exception 'App\Router\RouterException' with message 'No route matches this name' in C:\xampp\htdocs\Grafikart\src\Router\Router.php:48 Stack trace: #0 C:\xampp\htdocs\Grafikart\index.php(13): App\Router\Router->url('posts.show', Array) #1 [internal function]: {closure}('48', 'salut-les-gens') #2 C:\xampp\htdocs\Grafikart\src\Router\Route.php(47): call_user_func_array(Object(Closure), Array) #3 C:\xampp\htdocs\Grafikart\src\Router\Router.php(39): App\Router\Route->call() #4 C:\xampp\htdocs\Grafikart\index.php(27): App\Router\Router->run() #5 {main} thrown in C:\xampp\htdocs\Grafikart\src\Router\Router.php on line 48
j'utilise l'autoloader.php parce que je ne comprend pas au debut de tuto le composer
et voila mon code
Autoloader.php
<?php
namespace App;
class Autoloader{
static function register(){
spl_autoload_register(array(__CLASS__, 'autoload'));
}
static function autoload($class){
if(strpos($class, __NAMESPACE__.'\\') === 0){
$class = str_replace(__NAMESPACE__, '', $class);
$class = str_replace('\\', '/', $class);
require 'src/'.$class.'.php';
}
}
}
?>
index.php
<?php
use \App\Autoloader;
require 'vendor/Autoloader.php';
Autoloader::register();
$router = new App\Router\Router($_GET['url']);
$router->get('/', function(){ echo "homepage"; });
$router->get('/posts', function(){ echo 'Tous les articles'; });
$router->get('/posts/:id-:slug', function($id, $slug) use ($router) { echo $router->url('posts.show', ['id' => 1, 'slug' => 'salut-les-gens']); }, 'posts-show')->with('id', '[0-9]+')->with('slug', '([a-z\-0-9]+)');
$router->get('/posts/:id', function($id){
?>
<form action="" method="post">
<input type="text" name="name">
<button type="submit">Envoyer</button>
</form>
<?php
});
$router->post('/posts/:id', function($id){ echo 'Poster pour l\'article '.$id.'<pre>'.print_r($_POST, true).'</pre>'; });
$router->run();
?>
Router.php
<?php
namespace App\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');
}
private 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();
}
}
var_dump($this->matches);
throw new RouterException('No matching routes');
}
public function url($name, $params = []){
if(!isset($this->namedRoutes[$name])){
throw new RouterException('No route matches this name');
}
return $this->namedRoutes[$name]->getUrl($params);
}
}
?>
RouterException.php
namespace App\Router;
class RouterException extends \Exception {
}
Route.php
<?php
namespace App\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 with($param, $regex){
$this->params[$param] = str_replace('(', '(?:', $regex);
return $this;
}
public function match($url){
$url = trim($url, '/');
// print_r($this->path);
$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(){
return call_user_func_array($this->callable, $this->matches);
}
public function getUrl($params){
$path = $this->path;
foreach($params as $k => $v){
$path = str_replace(":$k", $v, $path);
}
return $path;
}
}
?>
Bonjour,
Ça c'est du balançage de code ou je m'y connais pas...
L'erreur parle d'elle même, tu as créé toi même l'exception.
if(!isset($this->namedRoutes[$name])){
throw new RouterException('No route matches this name');
}