PHP-DI / Slim-Bridge

PHP-DI integration with the Slim framework
http://php-di.org/doc/frameworks/slim.html
MIT License
176 stars 38 forks source link

Slim 4 support #46

Closed fritsvt closed 5 years ago

fritsvt commented 5 years ago

Did some hacking today and managed to get it working in Slim 4.

I still need to clean things up and add/change tests but it all seems to work so far.

I had to change the way the App gets created because this seemed to have changed in Slim 4.

A basic example of booting up an app would now look like this:

<?php

use DI\Container;
use Slim\Factory\AppFactory;
use DI\Bridge\Slim\App;

require __DIR__ . '/../vendor/autoload.php';

// Create Container using PHP-DI
$container = new Container();

// Set container to create App with on AppFactory
AppFactory::setContainer($container);

// boot Slim4 app with DI
$app = App::boot($container);

Will continue to work on this in the coming days to hopefully get it all working.

EDIT: Changed the behaviour to still be the same as before. So the DI-Bridge App would accept the same parameters as the Appfactory::create method does. Only changed it so the container becomes the first parameter because that's the one people would use most.

Example would now look like:

<?php

use DI\Bridge\Slim\App;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Psr7\Response;

require __DIR__ . '/../vendor/autoload.php';

$app = new App;

$app->get('/{name}', function ($name, Request $request, Response $response) {
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->run();

So this would create a DI container for you

I removed the ErrorTest and ContainerTest because I think that behaviour changed. But please let me know if I'm wrong there.

I can't seem to fix one of the Travis checks. So if someone knows why that's failing please let me know.

@mnapoli when you have time, please let me know what you think :)

resolves #22

mnapoli commented 5 years ago

Thanks! I'm sorry I saw the PR yesterday and worked today on fixing the tests. It seems we have both fixed the test, sorry about that!

I managed to include some of your commit into that branch, I'll push a PR. I decided to mimic the behavior of the AppFactory from Slim, instead of extending App. I think that will be more in line with what's documented in Slim.

fritsvt commented 5 years ago

Alright, I wasn't sure what to do with the behaviour so I decided to keep it the same in the end. But I agree that keeping it similar to the framework is better. New PR looks good 👍

fritsvt commented 5 years ago

Closing this so v4 can move to #47