GithubHelp home page GithubHelp logo

flevour / sfdependencyinjectioncontainerplugin Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 86 KB

Fork of sfDependencyInjectionContainerPlugin

Home Page: http://www.symfony-project.org/plugins/sfDependencyInjectionContainerPlugin

License: MIT License

PHP 100.00%

sfdependencyinjectioncontainerplugin's Introduction

sfDependencyInjectionContainerPlugin
=================

The `sfDependencyInjectionContainerPlugin` packages the dependency injection container component into Symfony.


Installation
------------

  * Install the plugin

        $ symfony plugin:install sfDependencyInjectionContainerPlugin

  * Clear the cache

        $ symfony cache:clear

  * Activate the plugin in the `config/ProjectConfiguration.class.php`:

        [php]
        class ProjectConfiguration extends sfProjectConfiguration
        {
          public function setup()
          {
            $this->enablePlugins(array(
              /* ... */
              'sfDependencyInjectionContainerPlugin',
            ));
          }
        }

>**NOTE**
>This plugin must be enabled last to be able to register events and create services through the symfony event dispatcher.




Documentation
-------------

### Bind your own services

Thanks to the sfEventDispatcher bundled with Symfony, this plugin notify an event after the service container initialization.

To listen to the event, connect to that event name (in your ProjectConfiguration class for example):

    [php]
    $this->dispatcher->connect('service_container.load_configuration', array($this, 'listenToServiceContainerLoadConfiguration'));


Here is an implementation of a listener

    [php]
    public function listenToServiceContainerLoadConfiguration(sfEvent $event)
    {
      $container = $event->getSubject();
      $loader    = new sfServiceContainerLoaderFileYaml($container);
      $loader->load(dirname(__FILE__).'/services.yml');
    }



### Use services in your application

The plugin add two methods to your ProjectConfiguration and your actions, to ease the usage.

 * getServiceContainer()
 * getService()

Example:

    [php]
    public function executeIndex(sfWebRequest $request)
    {
      $sc   = $this->getServiceContainer();
      $mail = $sc->mail;
      // or
      $mail = $this->getService('mail');
    }



To know more about the dependency injection container component, please refer to the official documentation : (http://components.symfony-project.org/dependency-injection/).

sfdependencyinjectioncontainerplugin's People

Contributors

flevour avatar

Stargazers

Gun.io Robot avatar  avatar David Perez Vicens avatar Richard Scoop avatar Clement Herreman avatar Francesco Tassi avatar  avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.