Zend Framework debug console... chicken / egg problem

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Zend Framework debug console... chicken / egg problem

Post 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);
    
    }
}
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

Solution: stay away from Zend_Layout.
Alternatively, you can use a placeholder like the headScript plugins.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post by Luke »

That is what I'm using... am I using it wrong?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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!!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

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

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

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

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post by Luke »

Hey that is a great idea! I don't know why I hadn't thought of that!! Thanks guys :)
Post Reply