PHP code example of innmind / router
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\{
Router,
Any,
Method,
Endpoint,
Handle,
};
use Innmind\Http\{
ServerRequest,
Response,
Response\StatusCode,
};
$router = Router::of(
Any::of(
Method::post()
->pipe(Endpoint::of('/url{/template}'))
->pipe(Handle::of(static fn(ServerRequest $request, string $template) => Response::of(
StatusCode::ok,
$request->protocolVersion(),
))),
Method::delete()
->pipe(Endpoint::of('/resource/{id}'))
->pipe(Handle::of(static fn(ServerRequest $request, string $id) => Response::of(
StatusCode::ok,
$request->protocolVersion(),
))),
),
);
$response = $router(/* instance of ServerRequest */)->unwrap(); // Response
use Innmind\Router\{
Router,
Any,
Method,
Endpoint,
Handle,
};
use Innmind\HttpServer\Main;
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Http\{
ServerRequest,
Response,
Response\StatusCode,
};
use Innmind\Filesystem\Name;
use Innmind\Url\Path;
new class extends Main {
private Router $router;
protected function preload(OperatingSystem $os): void
{
$this->router = Router::of(Any::of(
Method::get()
->pipe(Endpoint::of('/image/{name}'))
->pipe(Handle::of(static fn(string $name) => self::loadFile(
$os,
$name,
))),
Method::get()
->pipe(Endpoint::of('/image/random'))
->pipe(Handle::of(static fn() => self::loadFile(
$os,
generateRandomName(),
))),
));
}
protected function main(ServerRequest $request): Response
{
return ($this->router)($request)->match(
static fn($response) => $response,
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/'))
->unwrap()
->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(),
),
);
}
}