1. Go to this page and download the library: Download wilaak/radix-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/ */
wilaak / radix-router example snippets
use Wilaak\Http\RadixRouter;
$router = new RadixRouter()
->add('GET', '/', function () {
echo 'Welcome home!';
})
->add('GET', '/greet/:name?', function ($name = 'World') {
echo "Hello, {$name}!";
});
$result = $router->lookup(
$_SERVER['REQUEST_METHOD'],
rawurldecode(strtok($_SERVER['REQUEST_URI'], '?')),
);
switch ($result['code']) {
case 200:
$result['handler'](...$result['params']);
break;
case 404:
http_response_code(404);
echo '404 Not Found';
break;
case 405:
header('Allow: ' . implode(',', $result['allowed_methods']));
http_response_code(405);
echo '405 Method Not Allowed';
break;
}
// Simple GET route
$router->add('GET', '/about', 'about');
// Multiple HTTP methods
$router->add(['GET', 'POST'], '/contact', 'contact');
// Any allowed HTTP method
$router->add($router->allowedMethods, '/maintenance', 'maintenance');
// Special fallback HTTP method (allowed or not)
$router->add('*', '/maintenance', 'maintenance');
// Required parameter
$router->add('GET', '/users/:id', 'get_user');
// Example requests:
// /users -> no match
// /users/123 -> ['id' => '123']
// You can have as many as you want, but keep it sane
$router->add('GET', '/users/:id/orders/:order_id', 'get_order');
$cacheFile = __DIR__ . '/route-cache.php';
if (!file_exists($cacheFile)) {
// You must only provide cache-safe handlers that can be
// exported by var_export() and restored with (), 'show']);
$router->add('GET', '/hello', 'my_function_name');
$router->add('GET', '/users/:id', [UserController::class, 'show']);
$router->add('GET', '/posts/:id', [
'uses' => [PostsController::class, 'show'],
'name' => 'posts.show',
]);
// ...continue building your routes...
// WARNING:
// Do not rely on the internal format of these public
// properties without locking your version first.
// Their structure may change at any moment and is
// only meant for route caching purposes.
$routes = [
$router->tree,
$router->static,
];
// NOTE:
// Care should be taken to avoid race conditions,
// ensure that the cache is written atomically so that
// each request can always load a valid cache file.
file_put_contents($cacheFile,
' return ' . var_export($routes, true) . ';'
);
}
$routes =
METHOD PATTERN HANDLER
------------------------------------------------------------
GET /about about
GET /archive/:year?/:month? list_archive
GET /assets/:resource+ serve_asset
GET /blog/:slug? view_blog
GET /contact contact
POST /contact contact
GET /downloads/:file* serve_download
* /maintenance maintenance
GET /users/:id get_user
METHOD PATTERN HANDLER
------------------------------------------------------------
GET /contact contact
POST /contact contact
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.