Implementing AJAX support in an MVC Framework

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

AlexC
Forum Commoner
Posts: 83
Joined: Mon May 22, 2006 10:03 am

Implementing AJAX support in an MVC Framework

Post by AlexC »

Hey there,

I'm wanting to add AJAX support to my MVC framework, however I can not really see it working (with any benefits) - probably down to my lack of knowledge of how it will/can work. I would have to make a request to a certain Controller's section/action correct, and that would be the same URL as if I was going to it directly via the browser? But, when this happens - it would load the entire framework again, and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page ... which takes away half of the reasoning for AJAX.

I'm really confused about this as you can probably tell, so if anyone could share some tips on how to do this with an MVC framework I'd really appreciate it.

Thanks,
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Run two different front controllers depending on whether the request is AJAX or not.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

...or call two different sets of templates from the same controllers!
nickvd wrote:

Code: Select all

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') define('IS_AJAX',true);
with something like:

Code: Select all

$template = '/path/to/template.php';
if (IS_AJAX) $template = '/xml'.$template;
AlexC
Forum Commoner
Posts: 83
Joined: Mon May 22, 2006 10:03 am

Post by AlexC »

That's great, should help a lot! Although, with that - it will still have to make a new MySQL connection and load the entire framework again wont it? Maybe I could use that $_SERVER['HTTP_X_REQUESTED_WITH'] to load an alternative, light-weight bootstrap and have a special ajax-controller class that would load extra parts of the bootstrap and set them up correctly for the ajax-controller ... hum, I have an idea :lol:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I don't see why you would have to load anything twice. Sounds like you need to rethink some of your design there.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Implementing AJAX support in an MVC Framework

Post by Christopher »

AlexC wrote:I'm wanting to add AJAX support to my MVC framework, however I can not really see it working (with any benefits) - probably down to my lack of knowledge of how it will/can work. I would have to make a request to a certain Controller's section/action correct, and that would be the same URL as if I was going to it directly via the browser?
"Ajax support" is a little bit of a vague term as that technology is just an alternate way to make HTTP requests to a server. If you wrote a database results pager using Ajax, the server side code would be identical to one that used a regulat GET request. However I think you are talking about using Ajax to deliver small packets of data to update the page. Given that definition, then you would not use the same controller/action request to create the page initially as you would for succeeding Ajax calls. You could have one or more Ajax actions in your controller or a different controller altogether.
AlexC wrote:But, when this happens - it would load the entire framework again,
The point of implementing a Front Controller is to minimize what is loaded each request -- in a structured way. A Front Controller is no by definition a part of MVC, but it is an elegant way to run controllers.
AlexC wrote:and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page ... which takes away half of the reasoning for AJAX.,
Again ... you would not have the same code servicing different requests. The goal is to have actions that efficiently serve only the data that is needed. This is achieved by good design of both the client and server code, and with things like caching.
(#10850)
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 sort of disagree...

IMO AJAX should be an unobtrusive upgrade to the interface, thus exactly duplicating a standard xhtml request in a slicker, faster way. In that light, the same controllers, security, methods, etc... should be used, but only a subset of data needs to be returned.

In the case of a pagination AJAX call, it should be made as if the user were clicking on the full, AJAX-less url, but redirected to a different "view" template for either XML of JSON, returning only the necessary data. That would skip all the headers, navigation, footers, all ancillary content. This will greatly speed up the transfer and page load times, but should NOT take any server shortcuts.

Besides, it saves you having to write separate controllers and methods for each action while ensuring your security model - it's win/win baby!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Kieran Huggins wrote:I sort of disagree...
I think we actually said the same thing in different ways...
(#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:
Kieran Huggins wrote:I sort of disagree...
I think we actually said the same thing in different ways...
I agree with arborint, you've been working too hard Kieran :wink:
Last edited by John Cartwright on Sun Jun 03, 2007 2:23 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

As usual, I sort of agree with Kieran... ;)
(#10850)
AlexC
Forum Commoner
Posts: 83
Joined: Mon May 22, 2006 10:03 am

Post by AlexC »

Hum, now I'm even more confused. I am right in thinking with AJAX, you create another HTTP request and then catch it's output and replace parts of the HTML will the new output? If that's right, then when I make the new request to a URL - it will be acting the same as if I had gone there via the direct URL, and so it would load the entire framework? ... I mean, if one of my AJAX actions/sections requires a MySQL Connection - would it have to make another MySQL connection, or can it use the current one etc?

Would it be possible for some of you to show some examples so I can understand what you're trying to get at (I learn better when seeing code).
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Re: Implementing AJAX support in an MVC Framework

Post by kyberfabrikken »

AlexC wrote: But, when this happens - it would load the entire framework again, and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page
Yes, an ajax'ed page will put a bit more load on your server.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

AlexC wrote:If that's right, then when I make the new request to a URL - it will be acting the same as if I had gone there via the direct URL, and so it would load the entire framework?
AJAX is essentially a clientside technology. It makes no difference to the number of pages loaded on the server, or how much work they have to do for data wrangling. If your workflow requires 20 steps to do something then it'll be 20 requests to the server whether you use traditional requests or AJAX. The advantage of AJAX however is that you only need to pass the data rather than including the layout as well. That means you're going to be saving bandwidth, CPU time, etc. Plus it's a nicer experience for the user.
But, when this happens - it would load the entire framework again, and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page
There's nothing stopping you passing the data for the initial setup with the layout and AJAX code that gets loaded on the first hit, so if you write the code properly you won't have to have two requests.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

onion2k wrote: There's nothing stopping you passing the data for the initial setup with the layout and AJAX code that gets loaded on the first hit, so if you write the code properly you won't have to have two requests.
True. Except, that's called javascript -- not ajax.
Last edited by kyberfabrikken on Sat Jun 02, 2007 12:20 pm, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

kyberfabrikken wrote:True. Except, that's called javascript -- not ajax.
I have no idea what point you're trying to make.
Post Reply