Previous versions (Splash <10):
v10 is work in progress. Use mouf/mvc.splash for now
Splash is a PHP router. It takes an HTTP request and dispatches it to the appropriate controller.
Want to get a feeling of Splash? Here is a typical controller:
use TheCodingMachine\Splash\Annotations\Get;
use TheCodingMachine\Splash\Annotations\URL;
class MyController
{
/**
* @URL("/my/path")
* @Get
*/
public function index(ServerRequestInterface $request)
{
return new JsonResponse(["Hello" => "World!"]);
}
}
Ok, so far, things should be fairly obvious to anyone used to PSR-7. The important parts are:
ServerRequestInterface
parameter, it will fill it the PSR-7 request object.ResponseInterface
.But Splash can provide much more than this.
Here is a more advanced routing scenario:
use TheCodingMachine\Splash\Annotations\Post;
use TheCodingMachine\Splash\Annotations\URL;
use Psr\Http\Message\UploadedFileInterface;
class MyController
{
/**
* @URL("/user/{id}")
* @Post
*/
public function index($id, $firstName, $lastName, UploadedFileInterface $logo)
{
return //...;
}
}
Look at the signature: public function index($id, $firstName, $lastName, UploadedFileInterface $logo)
.
$id
will be fetched from the URL (@URL("/user/{id}")
)$firstName
and $lastName
will be automatically fetched from the GET/POST parameters$logo
will contain the uploaded file from $_FILES['logo']
See the magic? Just by looking at your method signature, you know what parameters your route is expecting. The method signature is self-documenting the route!
Even better, Splash is highly extensible. You can add your own plugins to automatically fill some parameters of the request (we call those ParameterFetchers
).
You could for instance write a parameter fetcher to automatically load Doctrine entities:
/**
* Lo and behold: you can extend Splash to automatically fill the function parameters with objects of your liking
*
* @URL("/product/{product}")
* @Post
*/
public function index(My\Entities\Product $product)
{
return //...;
}
You might wonder: "How will Splash instantiate your controller?" Well, Splash will not instantiate your controller. Instantiating services and containers is the role of the dependency injection container. Splash connects to any container-interop compatible container and will fetch your controllers from the container.
This means that you must declare your controller in the container you are using. This is actually a good thing as this encourages you to not use the container as a service locator.
For best performance, Splash is caching the list of routes it detects. Unlike what can be seen in most micro-frameworks where the application slows down as the number of routes increases, in Splash, performance stays constant as the number of routes increases.
Found a typo? Something is wrong in this documentation? Just fork and edit it!