1. Go to this page and download the library: Download softwarepunt/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/ */
softwarepunt / minirouter example snippets
use SoftwarePunt\MiniRouter\MiniRouter;
$router = new MiniRouter();
$router->register('/ping', function () {
return 'pong';
});
use GuzzleHttp\Psr7\ServerRequest;
$request = ServerRequest::fromGlobals();
$response = $router->dispatch($request);
use Psr\Http\Message\RequestInterface;
$router->register('/show-user-agent', function (RequestInterface $request) {
return "Your user agent is: {$request->getHeaderLine('User-Agent')}";
});
use SoftwarePunt\MiniRouter\MiniRouter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$router = new MiniRouter();
$router->registerController('/greet/$name', SomeControllerClass::class, "targetMethod");
class SomeControllerClass
{
public function before(RequestInterface $request): ?ResponseInterface
{
// This method will be called *before* routing to the target method
// You can access the request and other variables here
if (!exampleAuthCheck())
return new Response(403, body: "Access denied!");
// If before() returns a response, routing is aborted and that response is returned
// This is a good place to handle things like pre-flight checks and authentication
return null;
}
public function targetMethod(string $name): ResponseInterface
{
return new Response(200, body: "Hello, {$name}!");
}
}
// 301 Moved Permanently
$router->registerRedirect('/from', '/to', true);
// 302 Found (Temporary redirect)
$router->registerRedirect('/from', '/to');
use MyProject\HomeController;
use GuzzleHttp\Psr7\ServerRequest;
use SoftwarePunt\MiniRouter\MiniRouter;
// ---------------------------------------------------------------------------------------------------------------------
// Init
---------------------------------------------------------------------------------------------------------------
// Main
$request = ServerRequest::fromGlobals();
$response = $router->dispatch($request);
// ---------------------------------------------------------------------------------------------------------------------
// Serve
http_response_code($response->getStatusCode());
foreach ($response->getHeaders() as $name => $values) {
foreach ($values as $value) {
header(sprintf('%s: %s', $name, $value), false);
}
}
echo $response->getBody()->getContents();
exit(0);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.