All Projects → juliangut → slim-php-di

juliangut / slim-php-di

Licence: BSD-3-Clause license
Slim Framework PHP-DI container integration

Programming Languages

PHP
23972 projects - #3 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to slim-php-di

Slim-Auth
A Slim 4 Skeleton.
Stars: ✭ 22 (+69.23%)
Mutual labels:  slim, php-di
SlimREST
An app skeleton for building a REST API with the Slim PHP Micro-Framework
Stars: ✭ 22 (+69.23%)
Mutual labels:  slim
container
An extremely lightweight DI container in PHP
Stars: ✭ 21 (+61.54%)
Mutual labels:  container-interop
slim-tutorial
Php Slim Framework Tutorial
Stars: ✭ 26 (+100%)
Mutual labels:  slim
GraphQLSuiteCRM
GraphQL SuiteCRM - Integrate with SuiteCRM using GraphQL
Stars: ✭ 18 (+38.46%)
Mutual labels:  slim
PHP-Frameworks-Bench
Popular PHP Frameworks Benchmark.
Stars: ✭ 28 (+115.38%)
Mutual labels:  slim
slim-boilerplate
A PHP boilerplate,for a fast API prototyping based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready
Stars: ✭ 58 (+346.15%)
Mutual labels:  slim
slim-swoole
Slim 3 MVC Skeleton With Swoole
Stars: ✭ 52 (+300%)
Mutual labels:  slim
rails-webpacker-vue
Пример Rails приложения с Webpacker и Vue.js
Stars: ✭ 19 (+46.15%)
Mutual labels:  slim
api rest slim framework
RESTFUL API o API REST con slim framework (PHP, MySql, PDO)
Stars: ✭ 14 (+7.69%)
Mutual labels:  slim
SLiM
SLiM is a genetically explicit forward simulation software package for population genetics and evolutionary biology. It is highly flexible, with a built-in scripting language, and has a cross-platform graphical modeling environment called SLiMgui.
Stars: ✭ 102 (+684.62%)
Mutual labels:  slim
DenseNet-Tensorflow
Reimplementation of DenseNet
Stars: ✭ 16 (+23.08%)
Mutual labels:  slim
dotfiles
my rice dump featuring meme bash aliases, minimal slim theme with waifus, some new i3 modes and a transparent status bar with blocks scripts.
Stars: ✭ 23 (+76.92%)
Mutual labels:  slim
rawphp
A powerful, robust and API-first, PHP framework that helps people from different PHP backgrounds work on the same project seamlessly. You can write Laravel, CakePHP, Slim, Symphone and Procedural PHP code inside it and it all works perfectly. Its the PHP Framework for everyone.
Stars: ✭ 31 (+138.46%)
Mutual labels:  slim
clockwork-firefox
Clockwork - php dev tools integrated to your browser - Firefox add-on
Stars: ✭ 22 (+69.23%)
Mutual labels:  slim
charcoal-app
Slim-based application (modules, routes / controllers and middlewares)
Stars: ✭ 13 (+0%)
Mutual labels:  slim
zend-di-config
PSR-11 PHP-DI container configurator for Laminas, Mezzio, ZF, Expressive applications or any framework that needs a PSR-11 container
Stars: ✭ 19 (+46.15%)
Mutual labels:  php-di
middleman-startae
A starter template ready to run on Netlify or Heroku. Comes with several helpers, partials and a nice basic structure to the HTML, Sass, Webpack and ES2015. Bottom line, a template that uses all the modern tools.
Stars: ✭ 43 (+230.77%)
Mutual labels:  slim
Slim-Console
Slim Framework Console
Stars: ✭ 26 (+100%)
Mutual labels:  slim
slim3-api-skeleton
Slim 3 API Skeleton with content negotiation, authentication, error handling, cache and performance in mind.
Stars: ✭ 12 (-7.69%)
Mutual labels:  slim

PHP version Latest Version License

Total Downloads Monthly Downloads

Slim Framework PHP-DI container integration

PHP-DI dependency injection container integration for Slim framework.

Installation

Best way to install is using Composer:

composer require juliangut/slim-php-di

Then require_once the autoload file:

require_once './vendor/autoload.php';

Usage

Use Jgut\Slim\PHPDI\ContainerBuilder to create PHP-DI container and extract Slim's App from it

use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use Slim\App;

$container = ContainerBuilder::build(new Configuration());

$app = $container->get(App::class);
// same as $app = \Slim\Factory\AppFactory::createFromContainer($container);

// Register your services if not provided as definitions
$container->set('service_one', function (ContainerInterface $container): ServiceOne {
    return new ServiceOne($container->get('service_two'));
});

// Set your routes

$app->run();

In order to register services in the container it's way better to do it in definition files

Configuration

use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;

$settings = [
    'ignorePhpDocErrors' => true,
    'compilationPath' => '/path/to/compiled/container',
];
$configuration = new Configuration($settings);

// Settings can be set after creation
$configuration->setProxiesPath(sys_get_temp_dir());
$configuration->setDefinitions('/path/to/definition/files');

$container = ContainerBuilder::build($configuration);

PHP-DI settings

  • useAutoWiring whether to use auto wiring (true by default)
  • useAnnotations whether to use annotations (false by default)
  • useDefinitionCache, whether to use definition cache (false by default)
  • ignorePhpDocErrors, whether to ignore phpDoc errors on annotations (false by default)
  • wrapContainer wrapping container (none by default)
  • proxiesPath path where PHP-DI creates its proxy files (none by default)
  • compilationPath path where PHP-DI creates its compiled container (none by default)

Refer to PHP-DI documentation to learn more about container configurations

In order for you to use annotations you have to require doctrine/annotations. Review documentation

Additional settings

  • definitions an array of paths to definition files/directories or arrays of definitions. Definitions are loaded in order of appearance
  • containerClass container class used on the build. Must implement \Psr\Container\ContainerInterface, \DI\FactoryInterface and \DI\InvokerInterface (\Jgut\Slim\PHPDI\Container by default)

Container array access shorthand

Default \Jgut\Slim\PHPDI\Container container allows shorthand array access by concatenating array keys with dots. If any key in the chain is not defined, normal Psr\Container\NotFoundExceptionInterface exception is thrown

use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;

$container = ContainerBuilder::build(new Configuration([]));

$container->get('configs')['database']['dsn']; // given "configs" is an array
$container->get('configs.database.dsn'); // same as above

Notice

Be careful though not to shadow any array key by using dots in keys itself

use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;

$container = ContainerBuilder::build(new Configuration([]));

$configs = [
    'foo' => [
        'bar' => [
            'baz' => 'shadowed!', // <== watch out!
        ],
    ],
    'foo.bar' => 'bingo!',
];
$container->set('configs', $configs);

$container->get('configs.foo.bar'); // bingo!
$container->get('configs.foo.bar.baz'); // NotFoundExceptionInterface thrown

The easiest way to avoid this from ever happening is by NOT using dots in array keys

Invocation strategy

By default, slim-php-di sets a custom invocation strategy that employs PHP-DI's Invoker to fulfill callable parameters, it is quite handy and lets you do things like this

use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;
use Psr\Http\Message\ResponseInterface;
use Slim\App;

$container = ContainerBuilder::build(new Configuration([]));

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

$app->get('/hello/{name}', function (ResponseInterface $response, string $name, \PDO $connection): ResponseInterface {
    // $name will be injected from request arguments
    // $connection will be injected from the container

    $response->getBody()->write('Hello ' . $name);

    return $response;
});

$app->run();

If you prefer default Slim's Slim\Handlers\Strategies\RequestResponse strategy or any other of your choosing you only have to set it in a definition file

use Slim\Handlers\Strategies\RequestResponse;
use Slim\Interfaces\InvocationStrategyInterface;

use function DI\create;

return [
    InvocationStrategyInterface::class => create(RequestResponse::class),
];

Migration from 2.x

  • Minimum Slim version is now 4.7
  • Container only provides implementations of the interfaces needed to instantiate an App. Refer to Slim's documentation
  • You can extract Slim's App directly from container or seed AppFactory from container
  • Slim's App is not extended anymore
  • ArrayAccess and magic methods on default container are deprecated and will be removed on next major release. Use PSR-11 and PHP-DI's methods instead

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].