Page 1 of 1

Zend Framework debug console... chicken / egg problem

Posted: Mon Jun 02, 2008 4:09 pm
by Luke
I have decided to create a controller plugin / action helper for my zend framework application that renders a debug console below my application if so told by my config object. I'm trying to make it show all the css / scripts / etc. that are being used in the current request. The problem is that styles are added in the view:

Example view (using layout)

Code: Select all

<!-- add a css file and javascript to this layout -->
<?= $this->headLink()->appendStylesheet($this->url(array('stylesheet' => 'base.css'), 'stylesheets')); ?> 
<?= $this->headScript()->appendFile($this->url(array('script' => 'image-gallery.js'), 'javascripts')); ?> 
<h2>Image Gallery</h2>
<ul class="gallery">
    <?= $this->partialLoop('image.phtml', $img) ?>
</ul>
Layout looks like:

Code: Select all

<?= $this->doctype('HTML4_STRICT'); ?> 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <?= $this->headTitle("Some Application Title"); ?> 
        <?= $this->headStyle(); ?>
        <?= $this->headScript(); ?> 
        <?= $this->headLink()->appendStylesheet($this->url(array('stylesheet' => 'base.css'), 'stylesheets')); ?> 
    </head>
    <body>
        <div id="container">
            <h1><?= $this->link('Home'); ?></h1>
 
<?= $this->layout()->content; ?> 
 
        </div> <!-- /container -->
        <?= $this->placeholder('debug'); ?> 
        <!-- start javascripts (loaded at end of body so as not to hold up the rest of the page during load) -->
        <?= $this->inlineScript(); ?> 
        <!-- end javascripts -->
    </body>
</html>
 
My problem is that since the links / scripts are added in the view, when my plugin requests the view object, the view template hasn't been rendered, so it can't see the newly added scripts / stylesheets. OK, no problem, I'll just make my plugin come later in the plugin stack...

bootstrap.php

Code: Select all

// ..snip
$front->registerPlugin(new Q_Controller_Plugin_Debug($view), 1000); // viewRenderer is at like 99 or 100, so set to 1000 to make sure it comes as late as possible
// ..snip
Well that works just great except that now the view is rendered before the debug plugin gets to add its view rendering to the debug placeholder... so wtf do I do??

My plugin (in case u wanted to see it)

Code: Select all

 
class Q_Controller_Plugin_Debug extends Zend_Controller_Plugin_Abstract
{
    protected $view;
    public function __construct(Zend_View_Abstract $view) {
    
        $this->view = $view;
    
    }
    public function postDispatch(Zend_Controller_Request_Abstract $request) {
    
        $front = Zend_Controller_Front::getInstance();
        $this->view->headLink()->appendStylesheet($this->view->url(array('stylesheet' => 'debug.css'), 'stylesheets'));
        $output = $this->view->render('elements/debug.phtml');
        $this->view->placeholder('debug')->set($output);
    
    }
}
 

Re: Zend Framework debug console... chicken / egg problem

Posted: Mon Jun 02, 2008 4:38 pm
by Eran
Solution: stay away from Zend_Layout.
Alternatively, you can use a placeholder like the headScript plugins.

Re: Zend Framework debug console... chicken / egg problem

Posted: Mon Jun 02, 2008 6:11 pm
by Luke
That is what I'm using... am I using it wrong?

Re: Zend Framework debug console... chicken / egg problem

Posted: Mon Jun 02, 2008 6:52 pm
by Eran
In my opinion Zend_Layout got a little too complicated to be of real use. The problems you are encountering are a reflection of that. The two-step view pattern is a little too abstract for web use in my opinion.

And I meant use a placeholder for your debug console that will be rendered on the second pass of the view rendering, similar to how the headScript plugins are used.

Re: Zend Framework debug console... chicken / egg problem

Posted: Mon Jun 02, 2008 7:14 pm
by Luke
Interesting... I'll take a look at how they are used (hadn't thought of that). Thanks!

Oh and I agree about them being complicated... just about everything about the zend framework is more complicated than it needs to be. I wish they'd adopt some of the django philosophy... don't add something unless its perfectly simple!!

Re: Zend Framework debug console... chicken / egg problem

Posted: Tue Jun 03, 2008 8:12 pm
by Ambush Commander
What I'd do is move the debug dialog creation to the very end, use output buffering to grab the HTML page and insert it in. Since it's debug, I don't mind if it's a nasty hack.

Re: Zend Framework debug console... chicken / egg problem

Posted: Wed Jun 04, 2008 2:55 am
by Maugrim_The_Reaper
Almost what Ambush Commander said ;). Grab the response body and throw in what you need using the DOM. The plugin can already access the Response object after dispatching. Grab body, apply DOM, make merry. The problem you're meeting is that you're trying too hard to hook past Zend_Layout with optional output which is difficult at the best of times, even with Placeholders. Since it's debugging, use your plugin to post-filter the output. DOM makes it easy enough. If you want extra points make it a plugin, which applies discrete Filter objects ;). I'm thinking of deflate/gzip on output for example.

Re: Zend Framework debug console... chicken / egg problem

Posted: Wed Jun 04, 2008 4:52 am
by Luke
Hey that is a great idea! I don't know why I hadn't thought of that!! Thanks guys :)