_posts/2019-08-01-slim-4.0.0-release.md
We are excited to announce the Slim 4.0.0 release. Please direct all your feedback for this release to the Slim 4 Release Feedback Thread. The new docs are located here.
Note: Travis-CI is configured to be triggered automatically at least every 24 hours.
For this major release the focus was on the following:
App instance due to the higher complexity of the increased modularitydefault_mimetype to an empty string, so you need to set it yourself in php.ini or your app using ini_set('default_mimetype', '').App::$settings have been removed, multiple middleware have been implemented to replace the functionality from each individual settings.Slim\Middleware\RoutingMiddleware. By default routing will be performed last in the middleware queue. If you want to access the route and routingResults attributes from $request you will need to add this middleware last as it will get executed first within the queue when running the app. The middleware queue is still being executed in Last In First Out (LIFO) order. This replaces the determineRouteBeforeAppMiddleware setting.Slim\Middleware\OutputBufferingMiddleware. This replaces the outputBuffering setting.Slim\Middleware\ContentLengthMiddleware. This replaces the addContentLengthHeader setting.RouterInterface component has been split into 4 interfaces RouteCollectorInterface, RouteParserInterface, RouteResolverInterface and DispatcherInterface.MiddlewareInterface signature process(Request $request, RequestHandler $handler). We no longer support the double-pass function ($request, $response, $next) signature. You can still use callables with the signature function (Request $request, RequestHandler $handler) {} to create middleware or use an invokable class with that signature.App now implements the PSR-15 RequestHandlerInterface. Use $app->handle($request) instead of $app($request).$app to the RouteGroup callable has been removed. RouteCollectorProxy gets injected into the RouteGroup's callable instead. $app->group('/group', function (RouteCollectorProxy $group) { $group->get(...); }App::subRequest() method has been removed. You can perform sub-requests via $app->handle($request) from within a route callable.SlimException. New HttpException have been implemented, namely HttpBadRequestException, HttpForbiddenException, HttpInternalServerErrorException, HttpMethodNotAllowedException, HttpNotFoundException, HttpNotImplementedException and HttpUnauthorizedException. You can also create your own by extending HttpException or HttpSpecializedException.App::map().CallableResolverTrait and MiddlewareAwareTrait no longer exist.AppFactory and ServerRequestCreatorFactory to facilitate App and ServerRequest creation. We currently support 4 PSR-7/ServerRequest creator combinations (Slim-Psr7, Nyholm PSR-7 & Nyholm PSR-7 Server, Guzzle PSR-7 and Guzzle HTTP Factory and Zend-Diactoros.ErrorMiddleware and RoutingMiddlewareRouteContext to enable access to the current route, route parser, and routing resultsErrorHandler componentErrorRendererInterface changed to use invokable pattern to leverage CallableResolverRouteParser::pathFor() and RouteParser::relativePathFor() are deprecated. Use RouteParser::urlFor() and RouteParser::relativeUrlFor()AppFactory to enable PSR-7 implementation and ServerRequest creator auto-detectionRouteCollectorProxyInterface which extracts all the route mapping functionality from app into its own interfaceRouteParserInterface and decouple FastRoute route parser entirely from core. The methods relativePathFor(), urlFor() and fullUrlFor() are now located on this interfaceDispatcherInterface and decouple FastRoute dispatcher entirely from core. This enables us to swap out our router implementation for any other routerRouteCollector::fullUrlFor() to give the ability to generate fully qualified URLsroutingResults request attribute to hold the results of routingRouteCollector::pushGroup(),RouteCollector::popGroup() which gets replaced by RouteCollector::group()RouteCollector::pathFor() which gets replaced by RouteCollector::urlFor() preserving the orignal functionalitydefault_mimetype to an empty string, so you need to set it yourself in php.ini or your app using ini_set('default_mimetype', '');determineRouteBeforeAppMiddleware setting is removed. Add RoutingMiddleware() where you need it nowaddContentLengthHeader setting is removedSlim\Http has been removed and Slim now depends on the separate Slim-Http componentoutputBuffering setting is removedApp::setContainer()App::__construct()Router::setCacheFile()Don't have Composer? It's easy to install by following the instructions on their download page.
We recommend you install Slim with Composer.
Navigate into your project's root directory and execute the bash command
shown below. This command downloads the Slim Framework and its third-party
dependencies into your project's vendor/ directory.
composer require slim/slim "^4.0"
Before you can get up and running with Slim you will need to choose a PSR-7 implementation that best fits your application.
In order for auto-detection to work and enable you to use AppFactory::create() and App::run() without having to manually create a ServerRequest you need to install one of the following implementations:
composer require slim/psr7
composer require nyholm/psr7 nyholm/psr7-server
composer require guzzlehttp/psr7 http-interop/http-factory-guzzle
composer require zendframework/zend-diactoros
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write('Hello world!');
return $response;
});
$app->run();
@l0gicgate created an app skeleton with a DDD style directory structure, example files and test coverage. It is a very opinionated and we're not sure yet if it is the right fit for a skeleton but we would like some feedback. You can clone the skeleton and try it out:
git clone https://github.com/l0gicgate/Slim-Skeleton.git
git checkout 4.x
@adriansuter created an MVC skeleton. You can clone the skeleton and try it out:
git clone https://github.com/adriansuter/Slim4-Skeleton.git