Skip to content
Art edited this page Jun 21, 2017 · 10 revisions

You are convinced by the dependency injection container of Symfony2, but you prefer stay with symfony1 for now. Here your dreams come true.
This fork integrate the discret dependency injection component as natural feature of the framework.

Define your services in services.yml files. As others config files, can be located in these places :

  • app/*/config/services.yml
  • config/services.yml
  • plugins/*/config/services.yml

Example of YAML definition file

Services definition is environment aware (unlike Symfony2).

# */services.yml
all:
  parameters:
    auth.host: http://example.com/auth.php
  services:
    auth:
      class: myAuthService
      arguments:
        - %auth.host%
      calls:
        - [ setEventDispatcher, [ @sf_event_dispatcher ] ]

dev:
  parameters:
    auth.host: http://localhost/auth.php

test:
  services:
    auth:
      class: myAuthServiceMock

See the full documentation of the YAML format.

Core services

The symfony core already contains some services (defined in symfony/lib/config/config/services.yml):

  • sf_formatter
  • sf_event_dispatcher
  • sf_logger
  • sf_user
  • sf_filesystem (operations are automatically logged)

These services can be used to define your own services.

If you see any service to add, please create an issue.

Call services

Services can be accessed from sfContext or any controller:

From an action

<?php

class myModuleActions extends sfActions
{
  public function executeIndex()
  {
    $fs = $this->getService('sf_filesystem');
    $fs->...
  }
}

Via sfContext

<?php

sfContext::getInstance()->getService('sf_filesystem');

From a task

<?php

class myTask extends sfTask
{
  public function run()
  {
    $fs = $this->getService('sf_filesystem');
    $fs->...
  }
}
Clone this wiki locally