1. Go to this page and download the library: Download mycodebox/minirouter 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/ */
mycodebox / minirouter example snippets
use MyCodebox\MiniRouter\Core\MiniRouter;
$router = new MiniRouter();
$router->get('/hello/{name}', function ($req, $res, $args) {
return $res->withHeader('Content-Type', 'text/plain')
->withBody('Hello, ' . $args['name'] . '!');
});
$router->dispatch();
// Route that responds to multiple methods:
$router->any(['GET', 'POST', 'put'], '/any-demo', function ($req, $res) {
return $res->withBody(['method' => $req->method]);
});
// Methods can be written in any case!
$router->get('/product/{id:\d+}', function ($req, $res, $args) {
// $args['id'] is guaranteed to be a number
});
$router->get('/blog/{year:\d{4}}/{slug}', function ($req, $res, $args) {
// $args['year'] = "2023", $args['slug'] = "my-article"
});
$router->get('/foo/{bar}', function ($req, $res, $args) {
// $args['bar'] accepts anything except slash
});
// ANY route example:
$router->any(['get', 'POST'], '/any', function ($req, $res) {
return $res->withBody(['method' => $req->method]);
});
// Methods can be written in any case!