Download the PHP package sanderdlm/mono without Composer
On this page you can find all versions of the php package sanderdlm/mono. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mono
Mono
Mono is a minimal, modern PHP framework in a single file.
It contains no custom implementations, but rather acts as a wrapper around the following great projects:
- Routing (using nikic/FastRoute)
- Dependency injection (using php-di/php-di)
- Middlewares (using relay/relay)
- Templating (using twigphp/wig)
- Data-to-object mapping (using cuyz/valinor)
This is the interface of the Mono
class:
Using just these methods, we have all the tools to build our application: ` Mono is not intended as a full-fledged framework, but rather as a proof-of-concept for small PHP apps. The goal is to show how far you can go by combining well-known libraries & PSR implementations.
The source code has comments and is easy to read.
1. Routing
You use $mono->addRoute()
to add all your routes. Same method signature as the underlying FastRoute method. Route handlers are closures by default, since this is mainly intended as a framework for small apps, but you can use invokable controllers as well.
Read about the route pattern in the FastRoute documentation. The entered path is passed directly to FastRoute.
The first argument to the closure is the always current request, which is a PSR-7 ServerRequestInterface object. After that, the next arguments are the route parameters.
When $mono->run()
is called, the current request is matched against the routes you added, the closure is invoked and the response is emitted.
1.1 Example with closure
1.2 Example with controller
Caching your routes in production is trivial. Pass a valid, writeable path for your cache file as a routeCacheFile
parameter to your Mono
object.
2. Dependency injection
When a Mono object is created, it constructs a basic PHP-DI container with default configuration. This means that any loaded classes (for example through PSR-4) can be autowired or pulled from the container manually.
You can fetch instances from the container with the get()
method on your Mono object.
Custom container
If you need to define custom definitions, you can pass a custom container to the Mono constructor. See the PHP-DI documentation for more information.
3. Middleware
Mono is built as a middleware stack application. The default flow is:
- Error handling
- Routing (route is matched to a handler)
- Your custom middlewares
- Request handling (the route handler is invoked)
You can add middleware to the stack with the addMiddleware()
method. Middleware are either a callable or a class implementing the MiddlewareInterface
interface. The middleware are executed in the order they are added.
`
You can find a bunch of great PSR-15 compatible middlewares already written in the middlewares/psr15-middlewares project. These can be plugged into Mono and used straight away.
4. Templating
If you want to use Twig, you have to pass the path to your templates folder in the Mono constructor.
Afterward, you can use the render()
method on your Mono object to render a Twig template from that folder.
`
5. Data-to-object mapping
This allows you to map data (for example, from the request POST body) to an object (for example, a DTO):
If you want to override the default mapping behaviour, define a custom Treemapper
implementation and set it in the container you pass to the Mono
constructor.
Example of a custom mapper config:
Other
Debug mode
Mono has a debug mode that will catch all errors by default and show a generic 500 response.
When developing, you can disable this mode by passing false
as the second argument to the Mono constructor. This will show the actual error messages and allow you to use dump
inside your Twig templates.
Folder structure & project setup
Getting started with a new project is fast. Follow these steps:
- Create a new folder for your project.
- Run
composer require sanderdlm/mono
. - Create a
public
folder in the root of your project. Add anindex.php
file. There is a "Hello world" example below. - Optionally, create a
templates
folder in the root of your project. Add ahome.twig
file. There is an example below. - Run
php -S localhost:8000 -t public
to start the built-in PHP server. - Start developing your idea!
public/index.php
:
templates/home.twig
:
If you're planning to keep things simple, you can work straight in your index.php. If you need to define multiple files/classes, you can add a src
folder and add the following PSR-4 autoloading snippet to your composer.json
:
You can now access all of your classes in the src
folder from your DI container (and autowire them!).
Extra packages
The following packages work really well with Mono. Most of them are quickly installed through Composer and then configured by adding a definition to the container.
-
vlucas/phpdotenv for environment variables
-
symfony/validator for validation of the request DTOs
- brefphp/bref for deploying to AWS Lambda
- symfony/translation for implementing i18n (symfony/twig-bridge also recommended)
All versions of mono with dependencies
twig/twig Version ^3.7
relay/relay Version ^2.1
php-di/php-di Version ^7.0
nikic/fast-route Version ^1.3
laminas/laminas-diactoros Version ^3.3
laminas/laminas-httphandlerrunner Version ^2.9
cuyz/valinor Version ^1.9