refactoring? my controllers to work with ajax requests

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

refactoring? my controllers to work with ajax requests

Post 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?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

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

Post by Luke »

interesting... I have never heard of $_SERVER['HTTP_X_REQUESTED_WITH']. I'm off to research this idea. thanks.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post by Luke »

not for this particular instance, no.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Luke »

well what do you guys think of my solution?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply