Migration from Slim3 to Slim4

Hello!

I am working on migration of my project from Slim3 to Slim4.
I have a question regarding this settings that was in Slim3

display_error_details: false
add_content_length_header: true
router_cache_file: false
determine_route_before_app_middleware: false
output_buffering: append
response_chunk_size: 4096
http_version: '1.1'

To handle this display_error_details I will add

if ($display_error_details) {
  $this->app->addErrorMiddleware(true, true, true);
}

To handle this output_buffering I will add

if ($output_buffering) {
  $this->app->add(new OutputBufferingMiddleware(new StreamFactory(), OutputBufferingMiddleware::APPEND));
}

To handle this add_content_length_header I will add

if ($add_content_length_header) {
  $this->app->add(new ContentLengthMiddleware());
}

To handle this add_content_length_header I will add

if ($router_cache_file) {
  $this->app->getRouteCollector()->setCacheFile(PATH['tmp'] . '/routes/routes.php');
}

But how to migrate this ? How to handle this in the code now ?

determine_route_before_app_middleware: false
response_chunk_size: 4096
http_version: '1.1'
determine_route_before_app_middleware

Routing is now done via the 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.

Retrieving the current route:

$route = \Slim\Routing\RouteContext::fromRequest($request)->getRoute();

response_chunk_size: 4096

The Slim 4 ResponseEmitter uses the value 4096 already as default value. As long as you don’t want to change this value, you don’t have to do anything.
If you want to change this default value, you have to replace this:

$app->run();

with…

use Slim\Factory\ServerRequestCreatorFactory;
use Slim\ResponseEmitter;
// ...

$request = ServerRequestCreatorFactory::create()->createServerRequestFromGlobals();
$response = $app->handle($request);

// Pass the new value here
$responseEmitter = new ResponseEmitter(8192);

$responseEmitter->emit($response);

http_version: '1.1'

The default http version is 1.1. So there is no need to change it.
With a middleware you can change the HTTP version from 1.1 to 1.0 as follows:

$response = $response->withProtocolVersion('1.0');
2 Likes