PHP code example of mougrim / yii2-container-configurator

1. Go to this page and download the library: Download mougrim/yii2-container-configurator 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/ */

    

mougrim / yii2-container-configurator example snippets



use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
use yii\di\Container;
...

Yii::$container->set(
    'containerConfigurator',
    function (Container $container) {
        $containerConfigurator = new ContainerConfigurator($container);
        $containerConfigurator->configure(


return [
    // your di container config
]


return [
    'app' => function () {
        return Yii::$app;
    },
]; 

Yii::$container->get('app');


use yii\web\Response;

return [
    'front.response' => [
        'class' => Response::class, // class name
    ],
];


use yii\web\Response;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;

return [
    'front.response' => [
        'class' => Response::class, // class name
        'type' => ContainerConfigurator::COMPONENT_TYPE_PROTOTYPE,
    ],
];


use yii\web\Response;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;

return [
    'front.response' => [
        'class' => Response::class, // class name
    ],
    'front.response-alias' => 'front.response',
];


Yii::$container->get('front.response-alias');


Yii::$container->get('front.response');

[
    'id' => 'front.request',
    'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
]

[
    'type' => ContainerConfigurator::ARGUMENT_TYPE_VALUE,
    'value' => 'some value',
],


use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;

return [
    'controllers.site' => [
        'class' => SiteController::class,
        'arguments' => [
            2 => [ // argument number
                'id' => 'front.request',
                'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
            ],
        ],
    ],
];


namespace frontend\controllers;

use yii\base\Module;
use yii\web\Controller
use yii\web\Request;

class SiteController extends Controller
{
    private $request;

    public function __construct(
        $id,
        Module $module,
        Request $request,
        array $config = []
    )
    {
        parent::__construct($id, $module, $config);
        $this->request = $request;
    }
}

    ...
    'controllerMap' => [
        ...
        'site' => 'controllers.site',
    ],
    ...


use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;

return [
    'controllers.site' => [
        'class' => SiteController::class,
        'properties' => [
            'request' => [
                'id' => 'front.request',
                'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
            ],
        ],
    ],
];


use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;

return [
    'controllers.site' => [
        'class' => SiteController::class,
        'call' => [
            'setRequest' => [ 
                [
                    'id' => 'front.request',
                    'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
                ],
            ],
        ],
    ],
];


use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;

return [
    'components.controller' => [
        'type' => ContainerConfigurator::COMPONENT_TYPE_PROTOTYPE,
        'properties' => [
            'app' => [
                'id' => 'app',
                'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
            ],
        ],
    ],
    'controllers.site' => [
        'class' => SiteController::class,
        'extends' => 'components.controller',
        'arguments' => [
            2 => [
                'id' => 'front.request',
                'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
            ],
        ],
    ],
];