/** * Permet de tester les méthodes statiques pour le router */ public function testRouterFactory() { Router::get('/', 'HomeController@index')->setName('index'); Router::post('/', 'HomeController@insert')->setName('insert'); Router::put('/{id:[0-9]+}', 'HomeController@update')->setName('update'); Router::delete('/{id:[0-9]+}', 'HomeController@remove')->setName('remove'); Router::group('/news', function () { Router::match(['get', 'post', 'patch', 'delete'], '/', 'NewsController@rest'); }); Router::any('/admin/news', 'AdminController@newsRest'); $this->assertCount(5, RouterManager::getInstance()->routes); $this->assertCount(3, RouterManager::getInstance()->routes['GET']); $this->assertCount(3, RouterManager::getInstance()->routes['POST']); $this->assertCount(2, RouterManager::getInstance()->routes['PUT']); $this->assertCount(2, RouterManager::getInstance()->routes['PATCH']); $this->assertCount(3, RouterManager::getInstance()->routes['DELETE']); $this->assertTrue(Router::parse('/', 'get', false)); $this->assertTrue(Router::parse('/1', 'put', false)); $this->assertFalse(Router::parse('/users', 'get', false)); }
<?php use PHQ\Routing\Router; Router::get('/', function () { return \PHQ\Http\Response::string('Welcome'); });
function url($name, array $params = []) { return Router::url($name, $params); }
/** * Lance le router et appel les middlewares et le controller */ public function run() { $path = realpath(__DIR__ . '/../../bin/routes'); $configFolder = Config::get('folder'); if ($configFolder) { $routePath = $configFolder->routes; if ($routePath && file_exists($routePath)) { $path = $routePath; } } $rdi = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); $rit = new RecursiveIteratorIterator($rdi); foreach ($rit as $file) { if ($file->getExtension() === 'php') { $pathFile = $file->getRealPath(); require "{$pathFile}"; } } $method = Request::method(); $routeReturn = !Request::isCLI(); $route = Router::parse(RequestManager::getInstance()->url, $method, $routeReturn); if ($route instanceof Route) { echo $route->run(); } }