If you are upgrading from version 2 to version 3, these are the significant changes that you need to be aware of.
Slim Core has removed Stop/Halt. In your applications, you should transition to using the withStatus() and withBody()
Example In Slim 2.x:
$app->get('/', function () { $app->halt(400, 'Bad Request'); });
And now in Slim 3.x:
$app->get('/', function ($req, $res, $args) {
return $res->withStatus(400)->write('Bad Request');
});
Slim v3 no longer has the concept of hooks. Hooks were removed as they duplicate the functionality already present in middlewares. You should be able to easily convert your Hook code into Middleware code.
In Slim v3 we have removed the HTTP-Caching into its own module Slim\Http\Cache ( https://github.com/slimphp/Slim-HttpCache )
In Slim v2.x one would use the helper function $app->redirect(); to trigger a redirect request. In Slim v3.x one can do the same with using the Response class like so.
Example:
$app->get('/', function ($req, $res, $args) {
return $res->withStatus(301)->withHeader("Location", "yournewuri");
});
Signature —- The middleware signature has changed from a class to a function New signature:
$app->add(function ($req, $res, $next) {});
Application middleware is executed as Last In First Executed (LIFE)
In v3.0 the concept of flash messages has been removed. There currently is no planned feature for this, but could likely be a part of the Cookies package.
In v3.0 cookies has been removed from core and moved to a separate component. See (https://github.com/slimphp/Slim-Http-Cookies)
In v3.0 we have removed the dependency for crypto in core.
Slim v3.0 requires PHP 5.5+
In v3.0 we have adopted a new callback signature:
$app->get('/', function (
\Psr\Http\Message\ServerRequestInterface $request,
\Psr\Http\Message\ResponseInterface $response,
array $args = null) {
//do stuff!
});
Slim now utilizes a new, more powerful router ( https://github.com/nikic/FastRoute )!
The syntax for adding route middleware has changed slightly. In v3.0:
php $app->get(…)->add($mw2)->add($mw1);
urlFor()
has been renamed pathFor()
and can be found in the router
object:
$router = $app->router;
$app->get('/', function ($request, $response, $args) use ($router) {
$url = $router->pathFor('home');
$response->write("<a href='$url'>Home</a>");
return $response;
})->setName('home');
Also, pathFor()
is base path aware.