PHP code example of fpeters / sf-dependency-injection-plugin

1. Go to this page and download the library: Download fpeters/sf-dependency-injection-plugin 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/ */

    

fpeters / sf-dependency-injection-plugin example snippets



// in a action.class
$container = $this->getServiceContainer();
$container->get('myCoolService');

// there is a short version of that
$this->getService('myCoolService');


<?= $this->getService('myCoolService')->doStuff(); 

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install
 php
# config/ProjectConfiguration.class.php

// Composer autoload
symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration
{
    // ...
}
 php
// Load Services from services.yml
$this->dispatcher->connect(
    'service_container.load_configuration',
    function (sfEvent $event) {
        // load  global config dir
        $loader = new YamlFileLoader($event->getSubject(), new FileLocator(sfConfig::get('sf_config_dir')));
        $loader->load('services.yml');
    }
);

$this->dispatcher->connect(
    'service_container.loaded',
    function (sfEvent $event) {
        // here you can do stuff when container is loaded
        $service = $event->getSubject()->get('myCoolService');
    }
);