2012年9月11日 星期二

symfony Service Container



http://symfony.com/doc/current/book/routing.html

In addition to using the logical name or the fully-qualified class name, Symfony supports a third way of referring to a controller. This method uses just one colon separator (e.g. service_name:indexAction) and refers to the controller as a service

http://symfony.com/doc/current/cookbook/controller/service.html

To refer to a controller that's defined as a service, use the single colon (:) notation. For example, suppose we've defined a service called my_controller and we want to forward to a method called indexAction() inside the service:
1
$this->forward('my_controller:indexAction', array('foo' => $bar));

http://symfony.com/doc/current/book/service_container.html

Service Container


The percent sign inside a parameter or argument, as part of the string, must be escaped with another percent sign:
<argument type="string">http://symfony.com/?foo=%%s&bar=%%d</argument>

Creating/Configuring Services in the Container


# app/config/config.yml
services:
    my_mailer:
        class:        Acme\HelloBundle\Mailer
        arguments:    [sendmail]

An instance of the Acme\HelloBundle\Mailer object is now available via the service container. The container is available in any traditional Symfony2 controller where you can access the services of the container via the get() shortcut method:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class HelloController extends Controller
{
    // ...

    public function sendEmailAction()
    {
        // ...
        $mailer = $this->get('my_mailer');
        $mailer->send('ryan@foobar.net', ...);
    }
}


沒有留言:

張貼留言