Bonjour,
j'ai essayé de suivre l'excellent tutoriel nord coder

j'y suis plus ou moins parvenu mais mon routeur ne fonctionne pas comme il devrait.
Visiblement l'appel de la méthode showId quand je veux passer un id en paramètre n’aboutis pas
Code : Sélectionner tout - Visualiser dans une fenêtre à part
$router->get('/article/:id','controllers\ArticlesController@showId');
Quelqu'un pourrait il m'aider SVP ? Merci

Voici mon index.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
<?php
require "../vendor/autoload.php";
$router = New router\Router($_SERVER["REQUEST_URI"]);
$router->get('/articles','controllers\ArticlesController@showAll');
$router->get('/article/:id','controllers\ArticlesController@showId');
$router->get('/','controllers\AccueilController@show');
$router->run();
Ma classe routeur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 
namespace router;
 
class Router
{
    private $url;
    private $routes = [];
    public function __construct($request_uri)
    {
        $this->url = trim($request_uri,'/');
    }
    public function get(string $path, string $action):void
    {
        $this->routes['GET'][]= New Route($path,$action );
    }
    public function run()
    {
      foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route)
      {
          if($route->matches($this->url))
          {
              $route->execute();
          }
      }
 
      //return header('HTTP/1.0 404 Not Found');
    }
 
}
Ma classe route
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
 
namespace router;
 
 
class Route
{
    private $path;
    private $action;
    private $matches;
 
    /**
     * @param $path
     * @param $action
     */
    public function __construct(string $path, string $action)
    {
        $this->path = trim($path,'/');
        $this->action = $action;
    }
    public function matches(string $url)
    {
        $path=preg_replace('#:([0-9]+)#','([^/])',$this->path);
        $pathToMatch="#^$path$#";
        if(preg_match($pathToMatch,$url,$matches))
        {
            $this->matches=$matches;
            return true;
        } else {
            return false;
        }
    }
    public function execute()
    {
        $params = explode('@',$this->action);
        $controller= new $params[0]();
        $method = $params[1];
        return isset($this->matches[1])?$controller->$method($this->matches[1]):$controller->$method();
    }
}
et enfin mon contrôleur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
namespace controllers;
 
class ArticlesController
{
    public function showAll(): void
    {
        ob_start();
        echo "j'affiche tous les articles";
        $content=ob_get_clean();
        require "../views/layout.php";
    }
    public function showId($id): void
    {
        ob_start();
        echo "j'affiche l'article N° $id";
        $content=ob_get_clean();
        require "../views/layout.php";
    }
}