Files
newspage/classes/Application/Router.php
2024-10-07 22:58:27 +02:00

76 lines
1.6 KiB
PHP

<?php
namespace Application;
use Application\Controller\HomeController;
use Application\Controller\ArticleController;
use Bramus\Router\Router as BramusRouter;
/**
* Simple router setup based on bramus/router.
*
* @link https://github.com/bramus/router
*/
class Router
{
private BramusRouter $router;
/**
* Constructor, setup router and routes
*/
public function __construct()
{
$this->setupRouter();
$this->setupRoutes();
}
/**
* Set up the router.
*
* @return void
*/
private function setupRouter(): void
{
$this->router = new BramusRouter();
$this->router->setNamespace('\Application\Controller');
}
/**
* Route-configuration will be done here.
*
* @return void
*/
private function setupRoutes(): void
{
$this->router->match('GET', '/', function () {
(new HomeController())();
});
$this->router->match('GET', '/messages', function () {
echo "<div>Lorem Ipsum Dolor</div>";
});
$this->router->match('GET', '/style.css', function () {
(new ScssCompiler())->compile();
});
$this->router->match('GET', '/debugbar/styles.css', function () {
#DebugBar::getInstance()->dumpCssAssets();
});
$this->router->match('GET', '/debugbar/javascript.js', function () {
#DebugBar::getInstance()->dumpJsAssets();
});
$this->router->match('GET', '/article/(\w+)-(\w+)', function ($name, $id) {
(new ArticleController())(['name' => $name, 'id' => $id]);
});
}
/**
* Run the routing and thus call the controllers.
*
* @return void
*/
public function serve(): void
{
$this->router->run();
}
}