Integrate both juliangut/slim-routing and php-di/slim-bridge

In my application I already use php-di/slim-bridge, where the app is created with
$app = \DI\Bridge\Slim\Bridge::create($container);

I would like to use also juliangut/slim-routing library to have php attributes defined routes and other benefits.

However, in the juliangut/slim-routing usage section, I see

$configuration = new Configuration([
    'sources' => ['/path/to/routing/files'],
]);
AppFactory::setRouteCollectorConfiguration($configuration);

// Instantiate the app
$app = AppFactory::create();

I don’t get how I can get the two libraries work together if $app is created in different ways.
Is there any way of achieving the integration of both libraries ?

Imagine your container definition:

definitions.php

return [
    Configuration::class => static function (ContainerInterface $container) {
        return new Configuration([
            'sources' => ['/path/to/routing/files'],
        ]);
    },
    App::class => static function (ContainerInterface $container) {
        AppFactory::setRouteCollectorConfiguration(
            $container->get(Configuration::class)
        );
        return AppFactory::createFromContainer($container);
    },
];

and $app instantiation:

bootstrap.php

use DI\ContainerBuilder;
use Slim\App;

$builder = new ContainerBuilder();
$builder->useAutowiring(true);
$definitions = require_once 'definitions.php';
$builder->addDefinitions($definitions);
$container = $builder->build();

$app = $container->get(App::class);
1 Like

thank you for opening my mind.
I achieved to set up what I wanted within the container.

Won’t it work just inserting the RouteCollectorConfiguration ?

$container = $containerBuilder->build();
$configuration = new Configuration([
    'sources' => ['/path/to/routing/files'],
]);
AppFactory::setRouteCollectorConfiguration($configuration);
AppFactory::setContainer($container);

// Instantiate the app
$app = AppFactory::create();

just wondering

Sure, but there is something called separation of concern. The way you wonder to apply, put together both application bootstrapping and injection definitions at the same code scope.

Look, this is certainly injection definition:

$configuration = new Configuration([
    'sources' => ['/path/to/routing/files'],
]);

you will have lots of such definitions to prepare, and I assume you will do it more or less like in my definitions.php file example.
Now you have to answer yourself a question, why Configuration object is so special that you decide to instantiate it within bootstraping scope and other in definitions scope?
I say there is nothing special in Configuration object, and you have to be consistent in your code.

BTW, you can do it optionally this way:

return [
    App::class => static function (ContainerInterface $container) {
        AppFactory::setRouteCollectorConfiguration(
            new Configuration([
                'sources' => ['/path/to/routing/files'],
            ])
        );
        return AppFactory::createFromContainer($container);
    },
];

and this is certainly justified and correct in case if Configuration object is used exclusively as a parameter of AppFactory::setRouteCollectorConfiguration method, meaning no other object will use it. In many cases it is not, that is why my example, for a presentation purpose is more general.

1 Like

Your first example was great. Thank you for your detailed reply.

I was just wondering of a simpler approach maybe because I’m an ol’ man already, and thus adhering fully to the KISS principle.

As you put it in your reply I (personally) won’t take offense of a Configuration object whatsoever in a app’s bootstrapping script, especially in term of concerns… but you are right start clean to enable a harmonious evolving is really important for long run and/or big project.

1 Like