I am working on my own MVC framework in PHP right now. I am trying to figure out how to deal with Ajax requests though…
A page is built using the URI http://www.domain.com/controller/method/params
So the URI is passed through a router class, that gets the controller and methods + params to use.
This sounds good so far, but I will also have a header and footer, maybe a sidebar even on the actual page and the the MVC will fill the main content part of the page.
When I make an Ajax request though, example to http://www.domain.com/user/create : controller=user method=create
I would then try to build the whole page again since it is sending a request to my app.
I am confused on how to deal with this properly?
There are some incomplete answers here. Here’s how you do it, it’s pretty simple…
What you do is check for the AJAX header and serve different content based on that. The AJAX header is:
It will be set to
XMLHttpRequestfor AJAX requests. This is a standard followed by all the major javascript libraries.So, knowing this we can check for the AJAX header in your method and display output accordingly.