PHP code example of passchn / cakephp-simple-di

1. Go to this page and download the library: Download passchn/cakephp-simple-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/ */

    

passchn / cakephp-simple-di example snippets


public function services(ContainerInterface $container): void
{
    Configure::load('app_di');

    $di = new DIManager([
        ServiceFactoryManager::class => Configure::readOrFail('DI.services'),
    ]);

    $di->addDependencies($container);
}

return [
    'DI' => [
        'services' => [
            NewsletterService::class => NewsletterServiceFactory::class,
            CheckoutService::class => CheckoutServiceFactory::class,
            PaymentService::class => fn () => new PaymentService(),
        ],
    ],
];

class ExamplesController {
    
    public function someAction(NewsletterService $service): Response 
    {
        $service->doSomething();
    }
}

\SimpleDI\Module\ServiceLocator\ServiceLocator::getContainer(); // the container instance
\SimpleDI\Module\ServiceLocator\ServiceLocator::get(NewsletterService::class); // the service

// in your Application::services()
ServiceLocator::setContainer($container);