1. Go to this page and download the library: Download innmind/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/ */
innmind / router example snippets
use Innmind\Router\{
Route,
Route\Name,
RequestMatcher\RequestMatcher,
UrlGenerator\UrlGenerator,
};
use Innmind\Http\{
Method,
ServerRequest,
};
use Innmind\UrlTemplate\Template;
use Innmind\Url\Url;
use Innmind\Immutable\{
Sequence,
Maybe,
};
$routes = Sequence::of(
Route::of(
Method::post,
Template::of('/url{/template}'),
)->named(Name::of('routeName')),
Route::of(
Method::delete,
Template::of('/resource/{id}'),
)->named(Name::of('anotherRoute')),
);
$requestMatcher = new RequestMatcher($routes);
$route = $requestMatcher(/* instance of ServerRequest */); // Maybe<Route>
$urlGenerator = new UrlGenerator($routes);
$urlGenerator(Name::of('routeName')); // Url or throws NoMatchingRouteFound
use Innmind\Router\{
Route,
RequestMatcher\RequestMatcher,
};
use Innmind\HttpServer\Main;
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Http\{
Method,
ServerRequest,
Response,
Response\StatusCode,
};
use Innmind\Filesystem\Name;
use Innmind\UrlTemplate\Template;
use Innmind\Url\Path;
new class extends Main {
private RequestMatcher $router;
protected function preload(OperatingSystem $os): void
{
$routes = Sequence::of(
Route::of(Method::get, Template::of('/image/{name}'))->handle(
fn($request, $variables) => $this->loadFile(
$os,
$variables->get('name'),
),
),
Route::of(Method::get, Template::of('/image/random'))->handle(
fn($request) => $this->loadFile(
$os,
generateRandomName(),
),
),
);
$this->router = new RequestMatcher($routes);
}
protected function main(ServerRequest $request): Response
{
return ($this->router)($request)->match(
static fn($route) => $route->respondTo($request),
static fn() => Response::of(
StatusCode::notFound,
$request->protocolVersion(),
),
);
}
private function loadFile(OperatingSystem $os, string $name): Response
{
return $os
->filesystem()
->mount(Path::of('some/private/folder/'))
->get(Name::of($name))
->match(
static fn($file) => Response::of(
StatusCode::ok,
$request->protocolVersion(),
null,
$file->content(),
),
static fn() => Response::of(
StatusCode::notFound,
$request->protocolVersion(),
),
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.