2
votes

I'm trying to understand the workflow of Zend application. For tests i've defined:

Controller - IndexController.php:

public function preDispatch() {
        error_log('IndexController::preDispatch');
        echo 'IndexController::preDispatch <br />'; }

public function init() {       
        error_log('IndexController::init');
        echo 'IndexController::init <br />'; }

public function indexAction() {        
        error_log('IndexController::indexAction');
        echo 'IndexController::indexAction <br />'; }

View script - index.phtml:

<?php
    echo'index view script - echo';
    $this->title = "ZF Tutorial";
    $this->headTitle($this->title); ?>
<h3>index view script - content</h3> 

Layout script - layout.phtml

...
<body>
    <div id="content">  
    <?php 
    error_log('Layout1');
    echo $this->layout()->content ?>
        <h1>
            <?php 
            error_log('Layout2');
            echo $this->escape($this->title); ?>            
        </h1>
    </div>
</body>
...

And here is my confusion. The order of error_log output is different from i've got in the browser.

error_log output (the expected order):

  • IndexController::init
  • IndexController::preDispatch
  • IndexController::indexAction
  • Layout1
  • Layout2

the browser output:

  • IndexController::init
  • index view script - echo
  • index view script - content
  • IndexController::preDispatch
  • IndexController::indexAction
  • ZF Tutorial

Why do the output in controller is rendered AFTER view script? Is the echo results from preDispatch & indexAction buffered somehow to output view script content first?

1
You are messing with the framework if you are echoing something where you should not. init or preDispatch should not echo anything. the framework use buffers to prevent people from doing stupid stuffs like these , check ob_start() , ob_get_clean() methods in phpmpm

1 Answers

2
votes

Yes, any output during the dispatch process is captured by an output buffer and appended to the response. This might seem confusing, but you aren't supposed to output things in a controller directly (but if you did you would still want to see it). Your error_log list shows the order these things are executed which is all that really matters.