Download the PHP package kod/bootstrap-slim without Composer
On this page you can find all versions of the php package kod/bootstrap-slim. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kod/bootstrap-slim
More information about kod/bootstrap-slim
Files in kod/bootstrap-slim
Package bootstrap-slim
Short Description Library for bootstrapping a Slim based application.
License MIT
Informations about the package bootstrap-slim
Bootstrap SLIM
This package provides a library for bootstrapping Slim framework 4 application. (Release 1 is for Slim 3)
Purpose
- Centralize the application's initialisation
- Reuse existing configuration in unit tests
- Create on the fly new customized configuration for unit tests
- Reduce usage of
require
/include
statements in favour ofuse
statement
Installation
Add this package to require
section of the composer.json
Implementation in 3 steps
Bootstrap - step 1
On index.php
for handling public requests.
Of course, Bootstrap
class does not provide any route or middleware except native Slim RoutingMiddleware and ErrorMiddleware.
You need to extend it and declare your routes in Bootstrap::addAppRoutes()
and middleware stack in
Bootstrap::addAppMiddlewares()
methods. It can be done easily by declaring class names like in example below.
Declare application routes and middleware stack
None of those classes exists yet. So, let's start with the most important. Let's create our route.
Routing - step 2
You have 2 options for creating routes. If you are a fun of OOP you can create a class containing your routes by
extending RouteDefintions
abstract class and declare your routes in __invoke($app)
method of this class. As
said above, you can but you are not obliged to. The MUST is to implement the __invoke($app)
method in your class which
receives Slim\App
instance as a parameter. The instance of the class is executed like a function.
And here comes the 2nd option. You can declare your routes as Closure which gets Slim\App
instance as a parameter. The 2nd approach is quite
useful for unit tests if you need to create a route on the fly. But what am I saying? I'm getting obsolete. With php we can
create anonymous classes! ¡Ay, caramba!
Anyway, just keep in mind that in both cases the Slim\App
instance is passed as a parameter. That means you can
organize your routes in groups and attach middleware to some route or group.
Route as a class:
Application routes' declaration as a Closure
Middleware - step 3
You can create a middleware in 2 ways: as a class or a closure. To create it as a class simply extend
invokable Middleware
abstract class, implement your business logic in it and trigger its execution in
Middleware::__invoke( ServerRequestInterface $request,RequestHandlerInterface $handler)
method.
Slim 4 middleware implements PSR-15 Middleware Interface which means that you do not have to use Middleware
abstract
class but write your own implementing the PSR-15 Middleware Interface. That'll do the job as well.
Either way, the application container will be pushed into the constructor of the middleware class by
Slim\MiddlewareDispatcher
.In Middleware
derived classes the container will be stocked in $this->ci
property.
To create a middleware as a closure simply respect the function signature.
Middleware as invokable class
Application middleware declaration
Route/Group middleware
Slim allows to attach a middleware to a route or a group of routes. This can be done in routes' definition like in example below.
Container
Slim 4 has a breaking change. It does not have container anymore. But no worry! This package provides an implementation of
ContainerInterface
built on top of Pimple\Container
with same methods as in Slim 3. If your application relies on some other
container implementation simply provide the instance of your container to Bootstrap. Another way to customize the container
is to override Bootstrap::init
method in your Bootstrap class and declare it in there.
Unit Testing
The keyword in unit testing is UNIT. Once your application is well organized and weakly coupled unit testing is like playing LEGO with your code. You can assemble routes and middleware, declare them on the fly as you wish.
Slim dependency
Slim framework 4 now makes part of project dependencies due to quite sophisticated request/response forgery introduced
with the latest version. To get rid of that dependency and rely on the slim version installed on your project you need
to override Bootstrap::init
method and use AppFactory
and ServerRequestCreatorFactory
coming with your project.
All versions of bootstrap-slim with dependencies
psr/http-message Version ^1.0
psr/http-server-handler Version ^1.0
pimple/pimple Version ^3.2
slim/psr7 Version ^0.5.0
slim/slim Version ^4