slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Migrations guide #1284

Closed akrabat closed 8 years ago

akrabat commented 9 years ago

We need a migrations guide.

This issue it to note what needs to go into it.

  1. Replacement of stop()/halt() as per https://github.com/slimphp/Slim/issues/1266
codeguy commented 9 years ago

Lots of other things have been removed, too. Will add more as I think of them.

  1. Hooks
codeguy commented 9 years ago

Removal of HTTP caching methods into separate components.

codeguy commented 9 years ago

Relocation of \Slim\App::redirect into the Response class.

geggleto commented 9 years ago

Change in middleware signature

//From
class AuthMiddleware extends \Slim\Middleware
{
    public function call()
    { } 
}

Too
$app->add(function ($request,$response,$next) {});
codeguy commented 9 years ago

HTTP request and response changes to satisfy PSR-7

geggleto commented 9 years ago

Removal of some components from core into separate components Flash Messages Cookies

akrabat commented 9 years ago

urlFor() is in Router now

codeguy commented 9 years ago

New \Slim\App class name, and new \Slim\App constructor signature.

codeguy commented 9 years ago

New minimum PHP version required.

codeguy commented 9 years ago

Removal of crypto

codeguy commented 9 years ago

How to read and parse request bodies based on content type

geggleto commented 9 years ago

New Route Callback Signature

$app->get('/', function (\Psr\Http\Message\ServerRequestInterface $request,
                             \Psr\Http\Message\ResponseInterface $response, $args) {
//do stuff!
});
lalop commented 9 years ago

new url defintion for parameters

Edit: Good one! Note to self: be sure we link to FastRoute docs, too

geggleto commented 9 years ago

Read and parse request bodies based on content type:

require 'vendor/autoload.php';

$app = new Slim\App();

//You want to make sure this one is as close to app->run
//Middlewares are run Last In First Executed
$app->add(function (\Psr\Http\Message\ServerRequestInterface $request, 
\Psr\Http\Message\ResponseInterface $response $response, $next) {
    switch($request->getHeader('Content-Type')) {
        case 'your-content-type' : { //Keep in mind slim does define some basic parsers.
        }
        default : {
             $response->withStatusCode(400)->withBody("Unsupported Content-Type");
        }
    }
    return $response;
});

$app->run();
geggleto commented 9 years ago

Hooks: Should be converted into Middleware.

Route Middleware is added differently.

Previously

$app->get('/', $mw, $mw2, function () {});

Now

$app->get('/', function () {})->add($mw2)->add($mw);
geggleto commented 9 years ago

To get the container inside a route function, IE to replace the DI from slim 2.

$app->get('/', function () {
    $container = $this->getContainer();
});
geggleto commented 9 years ago

@akrabat @codeguy google doc for this? //cc @silentworks @lalop

akrabat commented 9 years ago

No. It's part of https://github.com/slimphp/Slim-Documentation/tree/gh-pages

geggleto commented 9 years ago

Optional Route Parameters need to be solidified

akrabat commented 9 years ago

urlFor => pathFor

Also, pathFor prepends a '/', so to create a fully qualified URL, you now need to do this:

    $router = $this->router;
    $uri = $request->getUri();
    $url = $uri->getScheme() . '://' . $uri->getHost()
            . $uri->getPort() ? ':' . $uri->getPort() : ''
            . rtrim($uri->getBasePath(), '/')
            . $router->pathFor('news-archive', ['year'=>'2015'], ['foo' => 'bar']);
geggleto commented 9 years ago

SLIM MIGRATION GUIDE June 27th 2015 Glenn Eggleton geggleto@gmail.com

Removal of Stop/Halt

Slim Core has removed Stop/Halt. In your applications, you should transition to using the withStatusCode() 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)->withBody(“Bad Request”); });

Hooks

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.

Removal HTTP Cache

Slim v3 no longer ships with HTTP Cache methods in core. It has been moved to its own separate repo (https://github.com/slimphp/Slim-HttpCache )

Changed Redirect

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”);
});

Middleware

Signature

The middleware signature has changed from a class to a function New signature: php $app->add(function ($req, $res, $next) {});

Execution

Application middleware is executed as Last In First Executed (LIFE)

Flash Messages

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

Cookies

In v3.0 cookies has been removed from core and moved to a separate component See (https://github.com/slimphp/Slim-Http-Cookies)

Removal of Crypto

In v3.0 we have removed the dependency for crypto in core

PHP Version

Slim v3.0 requires php 5.4+

Route Callbacks

In v3.0 we have adopted a new callback signature

$app->get('/', function (\Psr\Http\Message\ServerRequestInterface $request,
\Psr\Http\Message\ResponseInterface $response, $args) {
//do stuff!
});

New Router

Slim now utilizes a new more powerful router ( https://github.com/nikic/FastRoute )!

Route Middleware

The syntax for adding route middleware has changed slightly. In v3.0: php $app->get(…)->add($mw2)->add($mw1);

piotr-cz commented 9 years ago

How about putting the migration guide on wiki?

JoeBengalen commented 9 years ago

The wiki is not really being maintaned ...

Migration guide is in the v3 docs: http://www.slimframework.com/docs/start/upgrade.html

dopesong commented 9 years ago

@geggleto there is helper method withRedirect for redirects :+1: Also php version: 5.5+

geggleto commented 9 years ago

@dopesong Yeah I need to update the migrations docs this week.

geggleto commented 8 years ago

1) Add Page to explain how the Container works with callables 2) Add Page to explain what the Immutability of the PSR-7 objects means

akrabat commented 8 years ago

Added issues to Slim-Website.