This commit is contained in:
sermandm 2025-03-19 09:44:45 +01:00
parent 41e1a58107
commit f9f148cf65
6 changed files with 105 additions and 2 deletions

View File

@ -3,7 +3,8 @@
spl_autoload_register(function ($className) {
// Définir les mappings des namespaces vers leurs répertoires de base
$namespaceMap = [
'App\\Configure\\Routing\\' => __DIR__ . '/../routing/',
'Portfolio\\Configure\\Routing\\' => __DIR__ . '/../routing/',
'Portfolio\\Source\\Design\\Controller\\Home' => __DIR__ . '/../../source/design/controller/home/',
];
// Parcourir les namespaces définis pour trouver une correspondance

View File

@ -1 +1,42 @@
<?php
namespace Portfolio\Configure\Routing;
class Route
{
public $path;
public $action;
public $matches;
public function __construct($path, $action)
{
$this->path = trim($path, '/');
$this->action = $action;
}
public function matches(string $url)
{
$path = preg_replace('#:([\w]+)#', '([^/]+)', $this->path);
$pathToMatch = "#^$path$#";
if (!preg_match($pathToMatch, $url, $matches)) {
return false;
}
$this->matches = $matches;
return true;
}
public function execute()
{
$params = explode('@', $this->action);
$controller = new $params[0]();
$method = $params[1];
if (isset($this->matches[1])) {
return $controller->$method($this->matches[1]);
}
return $controller->$method();
}
}

View File

@ -1 +1,30 @@
<?php
namespace Portfolio\Configure\Routing;
class Router
{
public $url;
public $routes = [];
public function __construct($url)
{
$this->url = trim($url, '/');
}
public function get(string $path, string $action)
{
$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");
}
}

View File

@ -1,3 +1,10 @@
<?php
echo "Salut";
require_once __DIR__ . '/../configure/autoloader/autoload.php';
$router = new \Portfolio\Configure\Routing\Router(['url']);
$router->get('/', 'Portfolio\Source\Design\Controller\Home\Index@index');
$router->get('/home/:id', 'Portfolio\Source\Design\Controller\Home\Read\Read@read');
$router ->run();

View File

@ -0,0 +1,13 @@
<?php
namespace portfolio\source\design\home;
class Index
{
public function index() {
// return $this->show('index.php');
echo "index";
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace portfolio\source\design\home\read;
class Read
{
public function read(int $id) {
echo "post" . $id;
}
}