1

Topic: ZC32 - Custom Action Helpers for Firebug

I'd like to introduce Tawfek's sequel to his video covering Debugging in Firebug with the Zend Framework. If you're looking for a tutorial on how to write a custom Action Helper, or wishing to make your ajax development more robust with logging and profiling, then these 33 minutes will save you a pile of googling.

Topics Covered
  • Review of firebug

  • 2:00: writing a custom Action Helper

  • 5:30: configuring the Zend Bootstrap

  • 10:00: Testing the Action Helper Through Firebug

  • 12:30: Writing magic methods

  • 16:00: Setting up a JSON-friendly view

  • 18:00: Integrating Zend_Db

  • 26:00: Implementing jQuery

  • 29:00: Console Logging with Firebug and jQuery

2

Re: ZC32 - Custom Action Helpers for Firebug

Hi,

I like your videos, but what bothers me is that you do errors in each word as you write, if you doesn't have any physical handicap (in this case ignore this part), so pls try to focus on it.

The other stuff is that your english isn't good, but this doesn't bother me because your english is understandable and i'm very happy for every video you post, because In every video i discover something.

This post is intended as a positive criticism and I don't want to offend you. wink

So i want to thank you for your videos and I look forward to more videos.

Have i nice day.

3

Re: ZC32 - Custom Action Helpers for Firebug

Hey sNop,

Thanks for the feedback! Personally, I think that Tawfek's contributions to Zendcasts are great. While his English isn't perfect, I think that first and foremost, this is a learning portal and if he can come and do 3 videos about the Zend Framework (in comparison to the 30 that are done by myself, a native English speaker), it helps enrich the overall content of the site.

Furthermore, the topics he covers are areas that he obviously demonstrates competency. If you're not a fan of his voice, simply download the project file.

When I do my own videos, I have two monitors so that the code I'm writing has already been written and I'm literally transcribing it (that's why I look like I never make mistakes, something that's very far from the truth.)

I hope you enjoy the video that was posted today.

Best,
-
Jon Lebensold

4

Re: ZC32 - Custom Action Helpers for Firebug

hey sNop  ,
This is me tawfek and i would like to thank you for your positive criticism
and as Jon Said If you're not a fan of my voice, My video  ,  simply download the project file .
if you are really happy with zendcasts vedio  you should really re think how to spread it the world , aren't you ??
Jon & I hope you find this site informative

Jon ,
Thank you very much , i am really appreciate it
very much smile
tawfek

5

Re: ZC32 - Custom Action Helpers for Firebug

First off, thanks for the screen casts. I found the site recently and they've been helpful thus far (I'm somewhat new to Zend, but come from other frameworks).

My only comment right now would be that I don't like holding the individual action methods responsible for disabling the layout, flagging the view renderer, etc. Instead I put that logic in the post dispatch hook and set a flag in each action which should return a JSON response.

ex:

public function jsonAction() {
    $this->_isJsonRequest = TRUE;
    ...
}

public function postDispatch() {
    if ($this->_isJsonRequest === TRUE) {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->getHelper('layout')->disableLayout();
        // Anything else that needs to happen in an action returning a JSON response
    }
}

Not sure if this is the best way, but that's how I'm doing it. Could probably get more fancy by detecting AJAX requests via the headers sent rather than having to set a flag manually, but maybe another day.

6

Re: ZC32 - Custom Action Helpers for Firebug

By the way, my registration email was auto flagged as spam in gmail, FYI.

7

Re: ZC32 - Custom Action Helpers for Firebug

I had the opportunity to work with Maxime Bouroumeau-Fuseau on a project recently and he contributed this ActionHelper, which is my personal favourite way of handling this kind of problem. Maxime has a knack for leveraging PHPDoc comments (notice the @ajax detection in the preDispatch). It reminds me of Attributes in C#:

PHP Code:
 
<?php
 
/**
 * If a controller action has the @ajax tag in its doc comment, then
 * the output will be a json string of the controller's json property
 */
class App_Controller_Action_Helper_Ajax extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * @var bool
     */
    protected $_isAjax = false;
    
    /**
     * (non-PHPdoc)
     * @see library/Zend/Controller/Action/Helper/Zend_Controller_Action_Helper_Abstract#preDispatch()
     */
    public function preDispatch()
    {
        $this->_isAjax = false;
        $controller = $this->getActionController();
        $action = $this->getRequest()->getActionName();
        $methodName = $action . 'Action';
        
        if (!method_exists($controller, $methodName)) {
            return;
        }
        
        $class = new ReflectionClass(get_class($controller));
        $method = $class->getMethod($methodName);
        
        if (!preg_match('/@ajax/', $method->getDocComment())) {
            return;
        }
        
        $this->_isAjax = true;
        $controller->getHelper('layout')->disableLayout();
    }
    
    /**
     * (non-PHPdoc)
     * @see library/Zend/Controller/Action/Helper/Zend_Controller_Action_Helper_Abstract#postDispatch()
     */
    public function postDispatch()
    {
        if (!$this->_isAjax || !isset($this->getActionController()->json)) {
            return;
        }
        
        $json = $this->getActionController()->json;
        if (!is_string($json)) {
            $json = Zend_Json::encode($json);
        }
        
        $this->getActionController()->getHelper('viewRenderer')->setNoRender();
        header('Content-type: application/json');
        echo $json;
    }
}
 

8

Re: ZC32 - Custom Action Helpers for Firebug

That's clever, thanks for sharing. I'm disciplined with doc blocks so this fits in nicely with my style. Very cool.

9

Re: ZC32 - Custom Action Helpers for Firebug

its really good idea to use phpdoc blocks to control your code
frankly for me i had never think to use it like this
thanks for sharing jon smile

10

Re: ZC32 - Custom Action Helpers for Firebug

i personally like a lot your english accent with french influences

especially i like at the beginning of the videos when you pronounce your name (i never understand it:)