geekmusclay / router
A simple router, for learning and fun
0.6.0
2023-07-02 15:13 UTC
Requires
- php: ^8.0
- geekmusclay/di-container: ^0.1
- guzzlehttp/psr7: ^2.4
- psr/http-server-middleware: ^1.0
- thecodingmachine/safe: ^1.3
Requires (Dev)
- laminas/laminas-coding-standard: ^2.4
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^6.2
- thecodingmachine/phpstan-safe-rule: ^1.2
This package is auto-updated.
Last update: 2024-12-31 00:33:38 UTC
README
Setup
Simply clone this pckage and run composer install
command.
/!\ WARNING This package require
geekmusclay/di-container
currently under development To use this package you will have to donwload it, adn require it locally
Getting started
declare(strict_types=1); require '../vendor/autoload.php'; use Geekmusclay\DI\Core\Container; use Geekmusclay\Router\Core\Router; use GuzzleHttp\Psr7\ServerRequest; $container = new Container(); $router = new Router($container); $router->get('/', function () { echo 'Hello World !'; }); try { $router->run(ServerRequest::fromGlobals()); } catch (Exception $e) { dd($e->getMessage()); }
Routing
$router->get('/', function () { echo 'GET route'; }); $router->post('/', function () { echo 'POST route'; }); $router->put('/put', function () { echo 'PUT route'; }); $router->delete('/delete', function () { echo 'DELETE route'; });
Group routes
$router->group('/api/v1', function (RouterInterface $group) use ($router) { $group->get('/', function () { echo 'Welcome on api !'; }, 'api.v1.index'); $group->get('/coucou', function () { echo 'Coucou'; }, 'api.v1.coucou'); $group->get('/:id', function (int $id) { echo 'Coucou n°' . $id; }, 'api.v1.coucou.detail')->with([ 'id' => '[0-9]+', ]); $group->group('/sub', function (RouterInterface $subgroup) use ($router) { $subgroup->get('/', function () use ($router) { echo 'Sub index : ' . $router->path('api.v1.sub.index'); }, 'api.v1.sub.index'); $subgroup->get('/test', function () { echo 'Sub test'; }, 'api.v1.sub.test'); $subgroup->get('/:id', function (int $id) { echo 'Sub n°' . $id; }, 'api.v1.sub.detail')->with([ 'id' => '[0-9]+', ]); }); });
Using PHP 8 attributes
$router->register(MyController::class);
use Geekmusclay\Router\Attribute\Route; use Psr\Http\Message\ServerRequestInterface as Request; #[Route(path: '/prefixed')] class MyController { #[Route(path: '/', name: 'fake.index')] public function index() { return 'Index'; } #[Route(path: '/hello', name: 'fake.hello')] public function hello() { return 'Hello'; } #[Route(path: '/static', name: 'fake.static')] public static function staticHello() { return 'Hello'; } #[Route(path: '/:id-:slug', name: 'fake.complex', with: [ 'id' => '[0-9]+', 'slug' => '[a-z\-]+' ])] public function complex(Request $request, int $id, string $slug) { return 'Method: ' . $request->getMethod() . ', Id: ' . $id . ', Slug: ' . $slug; } }
License
This package is under MIT licence.
Have fun!