Componette

Componette

o2ps

o2ps / SlimNetteBridge 1.2.0

Slim Framework bridge for Nette DI

download-cloud-line composer require oops/slim-nette-bridge

Oops/SlimNetteBridge

Build Status Downloads this Month Latest stable

This package helps you quickly build a Slim Framework application, utilizing the power of Nette DI container.

THIS PACKAGE IS NO LONGER MAINTAINED!

As suggested in #6, you can use slimapi/slimapi instead.

Installation and requirements

$ composer require oops/slim-nette-bridge

Oops/SlimNetteBridge requires PHP >= 7.1.

Usage

Register the extension in your config file.

extensions:
    slim: Oops\SlimNetteBridge\DI\SlimExtension(%debugMode%)

Then configure it:

slim:
    settings:
        addContentLengthHeader: false
    configurators:
        - App\MyConfigurator
  • settings section can be used to override Slim's default settings;
  • configurators is a list of ApplicationConfigurator implementations which, in the same order as defined in the list, can add routes and middlewares to the instance of Slim\App.

Once you have configured the bridge, you can create a simple index.php script in your document root, using nette/bootstrap to build the container:

<?php

// include Composer autoloader
require_once __DIR__ . '/path/to/vendor/autoload.php';

// configure and create the DI container
$configurator = new Nette\Configurator();
$configurator->setTempDirectory(__DIR__ . '/path/to/temp');
$configurator->addConfig(__DIR__ . '/path/to/config.neon');
$container = $configurator->createContainer();

// run the configured Slim application
$container->getByType(Slim\App::class)->run();

Don't forget to configure your web server to pass the incoming requests to the index.php script.

  • 1.2.0 1.2.0

    Thanks to the effort of @radimvaculik in #5, this release supports (in fact, requires) Nette 3.0 🎉

  • 1.1.0 1.1.0

    We developers are lazy, and so should be our applications. ApplicationConfigurators can now register not only callbacks, but also service types directly. This means that where you formerly wrote

    namespace My\App;
    class Configurator implements ApplicationConfigurator
    {
        private $controller;
        public function __construct(Controller $controller)
        {
            $this->controller = $controller;
        }
    
        public function configureApplication(Slim\App $app)
        {
            $app->get('/route', $this->controller);
        }
    }

    you can now write

    namespace My\App;
    class Configurator implements ApplicationConfigurator
    {
        public function configureApplication(Slim\App $app)
        {
            $app->get('/route', Controller::class);
        }
    }

    The Controller is automatically fetched from the DI container as long as it is registered as a service. This way you can optimize your applications because dependencies are not instantiated unnecessarily.

    If you don't use invokable classes and want to point to a specific method of the class, you can write

    namespace My\App;
    class Configurator implements ApplicationConfigurator
    {
        public function configureApplication(Slim\App $app)
        {
            $app->get('/route', Controller::class . ':method');
        }
    }
  • v1.0.0 1.0.0

    After some time in production, I think this package is ready for a stable release. This release includes a fix in the internals which prevents conflicts between unprefixed services in the DI container, see #1. As a result, SlimNetteBridge uses its own implementation of the PSR-11 container interface, and therefore providing such implementation from your code is no longer necessary.

price-tag-2-line

Badges

guide-fill

Dependencies

php (>= 7.1.0)
nette/di (^3.0)
Componette Componette felix@nette.org