1. Go to this page and download the library: Download juliangut/slim-php-di library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
juliangut / slim-php-di example snippets
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();
use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;
$settings = [
'useAttributes' => 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);
use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;
$container = ContainerBuilder::build(new Configuration([]));
$configs = [
'database' => [
'dsn' => 'mysql://root:pass@localhost/my_ddbb',
],
];
$container->set('configs', $configs);
$container->get('configs')['database']['dsn'];
$container->get('configs.database.dsn'); // same as above
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 directly from the container
$response->getBody()->write('Hello ' . $name);
return $response;
});
$app->run();
use Slim\Handlers\Strategies\RequestResponse;
use Slim\Interfaces\InvocationStrategyInterface;
use function DI\create;
return [
InvocationStrategyInterface::class => create(RequestResponse::class),
];
use Symfony\Component\Console\Application;
use Jgut\Slim\PHPDI\Command\ListCommand;
/** @var \Slim\App $app */
$container = $app->getContainer();
$cli = new Application('Slim CLI');
$cli->add(new ListCommand($container));
$app->run();
composer
bash
php -f cli.php slim:container:list --help
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.