-
Notifications
You must be signed in to change notification settings - Fork 178
ServiceContainer
jeromemacias edited this page Jan 8, 2012
·
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
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.
The symfony core already contains some services (defined in symfony/lib/config/config/services.yml):
- sf_formatter
- sf_event_dispatcher
- sf_logger
- 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.
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->...
}
}