5.9. Using S2Container with Zend_Controller

5.9.1. Environment

  • PHP-5.3.2
  • Zend Framework v1.10.2
  • s2container.php-2.0.3

5.9.2. Creation of the project

Let's make the project directory by using Zend_Tool_Framework. Afterwards, the setting to use s2container.php is added. In the following SVN repository, there is a project directory made this time.

5.9.3. Setup of S2Container

Let's add the setting to use s2container.php for the project made with Zend_Tool_Framework.

Preparing the s2.php. 

Create the s2.php which is a setting file of s2container.php in the application/configs directory.

Creation of S2ActionHelper. 

Make the action helper which accesses S2Container in the action method.

Edit of Bootstrap.php. 

Set the use of S2ActionHelper with Bootstrap of the application.(_initActionHelper method)

Creation of var directory. 

Make the var directory to save the log file, the session file, and cash, etc.

5.9.4. Using S2Container in the Action method

When using the component of S2Container in the action method, you can touch it by the S2ActionHelper.

    public function addAction()
        echo $this->_helper->s2('Service_Calc')->add(1, 2);

Let's create the Service_Calc class int the application/services directory. The access from in the method of the action to S2Container can be accessed by way of the property of S2ActionHelper. The name specified for a property becomes a component name.

    public function add2Action()
        echo $this->_helper->s2->calc->add(1, 2);

For the above-mentioned, the component of the name "calc" is taken out from S2Container. However, when the component is taken from S2Container by the component name, it is necessary to register the component in S2Container in advance. S2ActionHelper can read the configuration file to construct S2Container in each action. The configuration file is located in "module/dicons/controller/action.php" The following file becomes the configuration file of the cdList2 action for the above-mentioned.

5.9.5. UnitTest of The Component

Let's create the UnitTest in the tests direcotry. The bootstrap method of Zend_Application is executed with tests/application/bootstrap.php, and the applications environment is constructed. Create UnitTest of the Service_Calc class in the tests/application/services directory as CalcTest.php. In the Service_CalcTest class, the service component to be tested is acquired from S2Contaienr in the setUp method

class Service_CalcTest extends PHPUnit_Framework_TestCase {

    public function testAdd() {
        $this->assertEquals(3, $this->service->add(1, 2));

    public function setUp() {
        $this->service = s2get('Service_Calc');

    public function tearDown() {
        $this->service = null;

The execution result of UnitTest is as follows.

% cd tests
% cat phpunit.xml
<phpunit bootstrap="application/bootstrap.php"/>
% phpunit application/services/CalcTest.php
PHPUnit 3.4.2 by Sebastian Bergmann.


Time: 0 seconds

OK (1 test, 1 assertion)

© Copyright The Seasar Foundation and the others 2005-2010, all rights reserved.