Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-18-2008, 07:36 AM
Junior Member
 
Join Date: Apr 2008
Posts: 7
Default Zend_Controller Initialization

Hi guys,

I have the following controller

PHP Code:
<?php

class IndexController extends Zend_Controller_Action
{
    
    function 
init()
    {
        echo 
'INDEX CONTROLLER';
    }    

    function 
indexAction()
    {
        
$this->_forward('test');
    }

    function 
testAction()
    {
    }
    
}
The strange result I get is that "init()" method is called twice when I access my index action. Is that correct behavior?
Also I notice the following if I use the action stack like this:
PHP Code:
$this->_helper->actionStack('testa''test');
        
$this->_helper->actionStack('testb''test');
        
$this->_helper->actionStack('testaa''test'); 
Which simply calls the "test" controller actions "testaa", "testb" and "testc".
However the Test::init() method is called each time regarding the class was already instantiate with the first method. Is that correct that "init()" is called each time the action is invoked from this controller? I think this is a bug anyway.
The problem is that if you have controller like this:

PHP Code:
<?php

class IndexController extends Zend_Controller_Action
{
    
    function 
init()
    {
        
$this->_helper->actionStack('test''index');
    }    

    function 
indexAction()
    {
    }

    function 
testAction()
    {
    }
    
}
when you invoke indexAction() the page will just hang on because when in init method invoking testAction via the action stack helper, it invokes init() for testAction(), then in init() action stack helper again invoke testAction() and so on in one endless loop.

Also I would ask for some advise how to invoke other actions parallel to requested one in the init() method. Of course I can put the action stack helper calls in each actions I would like to but duplication is not what I think DRY means

Regards
Boris
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-18-2008, 01:44 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

Yes the init() funciton is run for every request against the controller, so you run three actions from that controller during the request init() will run three times. __construct() runs only once and preDispatch() plugins will only run once as well I believe. init() is run per action call to facilitate tailoring the controller initialization for the action being requested. Its not a bug. If you create a endless loop with forward calls then YOU have created the logical error condition. To call an action without running init() you do $this->testAction(); A controller is still an object and can be used as one as long as your action definitions don't muck it up.

I don't think you can really get parallel execution out of Apache on *nix or *dows but I'm not sure about that.
__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk
Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-18-2008, 02:16 PM
Junior Member
 
Join Date: Apr 2008
Posts: 7
Default

I think you're almost there. However based on manual the init() is called only once from the constructor. I also checked the code and it's correct the init() is called once in the constructor for ZF 1.5 Action.php@line:129

However the phenomenon is still exist. Try out this code

PHP Code:
class IndexController extends Zend_Controller_Action
{
    
    function 
init()
    {
        echo 
'INDEX CONTROLLER';
    }    

    function 
indexAction()
    {
        
$this->_forward('test');
    }

    function 
testAction()
    {
    }
    

You should see text "INDEX CONTROLLER" twice. I can explain this only with that not only init() is run twice but and the _construct() itself.
Logically this means that the dispatcher create two instances of this controller and run them one after another. For me this is not correct as the both action shared one class and I expect they to share the same class so I will preserve context and re-init() will be not needed. To proof my words try this code:

PHP Code:
class IndexController extends Zend_Controller_Action
{
    
    public function 
__construct(Zend_Controller_Request_Abstract $requestZend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
        
parent::__construct($request$response$invokeArgs);
        echo 
'CONSTRUCTOR<br>';
    }
    
    function 
init()
    {
        echo 
'INDEX CONTROLLER';
    }    

    function 
indexAction()
    {
        
$this->_forward('test');
    }

    function 
testAction()
    {
    }
    

If you start it with ZF 1.5 you'll see "CONSTRUCTOR" twice.
Why it's needed to initialize same things twice I can't understand.

What to say more!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-18-2008, 02:27 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

it's because $this->_forward starts the dispatch process all over vs. $this->testAction() referencing the current object. _forward creates another request on the dispatch stack.
__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk
Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 10:08 AM
Member
 
Join Date: Aug 2007
Location: Sweden
Posts: 47
Send a message via MSN to Leif.Högberg
Default

The simple answer is that forward is not meant to be used to forward between actions in the same controller. Do what Elemental says and call the method directly.

Last edited by Leif.Högberg : 04-24-2008 at 10:09 AM. Reason: Typo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:45 AM.