Page 1 of 1

refactoring? my controllers to work with ajax requests

Posted: Thu Mar 22, 2007 3:53 pm
by Luke
I have an application that I want to ajaxify™. I am wondering what the best way to go about this would be. I was thinking of taking my existing actions and depending on whether or not a param called ajax was set, I would return a json response, otherwise, I'd just do the normal thing. Here is an example action:

Code: Select all

public function addAction()
    {
        if (!$this->_isLoggedIn()) return; // make sure user is logged in
        
        $project = $this->_session->Project;
        $action = 'question';
        
        if ($pos = $this->_processId($this->_post->getRaw('question_id')))
        {
            $project->goto($pos);
            $action = 'review'; // change where we're forwarding
        }

        $answer = $this->_validateAnswer();
        $project->addAnswer($answer);

        /**
         * If there were any issues with adding the answer, _validateAnswer
         * will have already taken care of the error messages, etc. so
         * it is safe to simply forward the user to the question action now
         */

        $this->_forward('project', $action);
        return;
    }
Now, I was thinking that in my Application Controller, I'd detect if the ajax parameter was set and if so, set $this->_ajax to true, and then doing something like:

Code: Select all

public function addAction()
    {
        if (!$this->_isLoggedIn()) return; // make sure user is logged in
        
        $project = $this->_session->Project;
        $action = 'question';
        
        if ($pos = $this->_processId($this->_post->getRaw('question_id')))
        {
            $project->goto($pos);
            $action = 'review'; // change where we're forwarding
        }

        $answer = $this->_validateAnswer();
        $project->addAnswer($answer);

        if ($this->_ajax)
        {
            // output ajax response
            return;
        }

        /**
         * If there were any issues with adding the answer, _validateAnswer
         * will have already taken care of the error messages, etc. so
         * it is safe to simply forward the user to the question action now
         */

        $this->_forward('project', $action);
        return;
    }
Is this a good idea? How would you guys go about it?

Posted: Thu Mar 22, 2007 4:23 pm
by nickvd
I currently use a dedicated ajax controller, that handles every ajax request.

However I'm developing from the start with ajax in mind, so it makes it a little easier to plan things out, instead of retrofitting an existing app to function with ajax.

I am also curious as to how you are detecting the ajax call. I used to add my own $_GET['ajax'] = true; type flag to the request, but since using jQuery, I've started to use

Code: Select all

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')

under the assumption that jQuery is the one adding the header.

Posted: Thu Mar 22, 2007 4:27 pm
by Luke
interesting... I have never heard of $_SERVER['HTTP_X_REQUESTED_WITH']. I'm off to research this idea. thanks.

Posted: Thu Mar 22, 2007 5:02 pm
by Kieran Huggins
I'm using subdomains for the first time to differentiate my output format - they're all CNAME'd to the same server, but json.x.com triggers json output, api.x.com triggers xml, m.x.com triggers a mobile layout...etc...

Would that work for you?

Posted: Thu Mar 22, 2007 5:27 pm
by Luke
not for this particular instance, no.

Posted: Thu Mar 22, 2007 6:57 pm
by Christopher
nickvd wrote:I currently use a dedicated ajax controller, that handles every ajax request.
I do the opposite. I add an 'action' method to the controller making the Ajax requests. I find that Ajax is used within a controller context. So if I have a form with the URL "/people/edit/" that needs Ajax data then I might make something like "/people/zipcode-feed/".

Posted: Thu Mar 22, 2007 7:06 pm
by Luke
well what do you guys think of my solution?

Posted: Thu Mar 22, 2007 7:23 pm
by Christopher
I think it is fine, but putting all the logic in one action may lead to cruft down the road. I prefer separate actions and pull out common code into other methods/classes.

Posted: Fri Mar 23, 2007 1:04 pm
by John Cartwright
arborint wrote:
nickvd wrote:I currently use a dedicated ajax controller, that handles every ajax request.
I do the opposite. I add an 'action' method to the controller making the Ajax requests. I find that Ajax is used within a controller context. So if I have a form with the URL "/people/edit/" that needs Ajax data then I might make something like "/people/zipcode-feed/".
I do the same thing, seems much cleaner then mixing two separate requests into a single action, especially because you will have to re-process the entire action.