1. Go to this page and download the library: Download sanderdlm/mono 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/ */
sanderdlm / mono example snippets
interface MonoInterface
{
// Render a Twig template
public function render(string $template, array $data): string;
// Add a middleware to the PSR-15 stack
public function addMiddleware(MiddlewareInterface|callable $middleware): void;
// Add a route to the FastRoute router
public function addRoute(string|array $method, string $path, callable $handler): void;
// Run the app
public function run(): void;
}
$mono = new Mono();
$mono->addRoute('GET', '/books/{book}', function(ServerRequestInterface $request, string $book) {
return new TextResponse(200, 'Book: ' . $book);
});
$mono->run();
class BookController
{
public function __construct(
private readonly Mono $mono
) {
}
public function __invoke(ServerRequestInterface $request, string $book): ResponseInterface
{
return new TextResponse('Book: ' . $book');
}
}
$mono = new Mono();
// By fetching the controller from the container, it will autowire all constructor parameters.
$mono->addRoute('GET', '/books/{book}', $mono->get(BookController::class));
$mono->run();
$mono = new Mono(routeCacheFile: sys_get_temp_dir() . '/mono-route.cache');
$mono = new Mono();
$mono->addRoute('GET', '/example', function() use ($mono) {
$result = $mono->get(SomeDependency::class)->doSomething();
return new JsonResponse($result);
});
$mono->run();
// Custom container
$builder = new DI\ContainerBuilder();
$builder->... // Add some custom definitions
$container = $builder->build();
$mono = new Mono(container: $container);
$mono->addRoute('GET', '/example', function() use ($mono) {
$result = $mono->get(SomeDependency::class)->doSomething();
return new JsonResponse($result);
});
$mono->run();
$mono = new Mono();
$mono->addMiddleware(function (ServerRequestInterface $request, callable $next) {
// Do something before the request is handled
if ($request->getUri()->getPath() === '/example') {
return new TextResponse('Forbidden', 403);
}
return $next($request);
});
$mono->addMiddleware(function (ServerRequestInterface $request, callable $next) {
$response = $next($request);
// Do something after the request is handled
return $response->withHeader('X-Test', 'Hello, world!');
});
$mono = new Mono(__DIR__ . '/templates');
$mono->addRoute('GET', '/example', function() use ($mono) {
$result = $mono->get(SomeDependency::class)->doSomething();
return new HtmlResponse($mono->render('example.twig', [
'result' => $result
]));
});
$mono->run();
class BookDataTransferObject
{
public function __construct(
public string $title,
public ?int $rating,
) {
}
}
$_POST['title'] = 'Moby dick';
$_POST['rating'] = 10;
$mono = new Mono();
$mono->addRoute('POST', '/book', function (
ServerRequestInterface $request
) use ($mono) {
/*
* $bookDataTransferObject now holds
* all the data from the request body,
* mapped to the properties of the class.
*/
$bookDataTransferObject = $mono->get(TreeMapper::class)->map(
BookDataTransferObject::class,
$request->getParsedBody()
);
});
$mono->run();
$customMapper = (new MapperBuilder())
->enableFlexibleCasting()
->allowPermissiveTypes()
->mapper();
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->addDefinitions([
TreeMapper::class => $customMapper
]);
$mono = new Mono(
container: $containerBuilder->build()
);
declare(strict_types=1);
use Mono\Mono;
o->addRoute('GET', '/', function() {
return new HtmlResponse($mono->render('home.twig', [
'message' => 'Hello world!',
]));
});
$mono->run();
// Custom container
$containerBuilder = new \DI\ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->addDefinitions([
// Build & pass a validator
ValidatorInterface::class => Validation::createValidatorBuilder()
->enableAttributeMapping()
->getValidator(),
]);
$mono = new Mono(
container: $containerBuilder->build()
);
// Generic sign-up route
$mono->addRoute('POST', '/sign-up', function(
ServerRequestInterface $request
) use ($mono) {
// Build the DTO
$personDataTransferObject = $mono->get(TreeMapper::class)->map(
PersonDataTransferObject::class,
$request->getParsedBody()
);
// Validate the DTO
$errors = $mono->get(ValidatorInterface::class)->validate($personDataTransferObject);
// Do something with the errors
if ($errors->count() > 0) {
}
});
// Create a new translator, with whatever config you like.
$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', [
'hello_world' => 'Hello world!',
], 'en');
// Create a custom container & add your translator to it
$containerBuilder = new \DI\ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->addDefinitions([
TranslatorInterface::class => $translator,
]);
$mono = new Mono(
container: $containerBuilder->build()
);
/*
* Use the |trans filter in your Twig templates,
* or get the TranslatorInterface from the container
* and use it directly in your route handlers.
*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.