1. Go to this page and download the library: Download geekmusclay/router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
geekmusclay / router example snippets
declare(strict_types=1);
Container;
use GuzzleHttp\Psr7\ServerRequest;
use Geekmusclay\Router\Core\Router;
use Psr\Http\Message\ServerRequestInterface;
$container = new Container();
$router = new Router($container);
$router->get('/', function (ServerRequestInterface $request): void {
var_dump($request->getQueryParams());
echo 'Hello World !';
});
$router->get('/hello', function (ServerRequestInterface $request): void {
$name = $request->getQueryParams()['name'] ?? null;
if (null === $name) {
echo 'Hello World !';
} else {
echo 'Hello ' . $name . ' !';
}
});
try {
$router->run(ServerRequest::fromGlobals());
} catch (Exception $e) {
die($e->getMessage());
}
$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';
});
$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]+',
]);
});
});
$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;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.