Take the 2-minute tour ×

I am in the process of learning the MVC pattern and building my own lightweight one in PHP

Below is a basic example of what I have right now.

I am a little confused on how I should handle AJAX requests/responses though.

In my example user controller below, If I went to www.domain.com/user/friends/page-14 in the browser, it would create a User object and call the friends method of that object

The friends method would then get the data needed for the content portion of my page.

My app would load a template file with a header/footer and insert the content from the object above into the middle of the page.

Now here is where I am confused, if a request is made using AJAX then it will call a page that will do the process over, including loading the template file. IF an AJAX call is made, I think it should somehow, just return the body/content portion for my page and not build the header/footer stuff.

So in my MVC where should I build/load this template file which will have the header/footer stuff? ANd where should I detect if an AJAX request is made so I can avoid loading the template?

I hope I am making sense, I really need help in figuring out how to do this in my MVC I am building. IUf you can help, please use some sample code

* Extend this class with your Controllers
* Reference to the model wrapper / loader functions via $this->model
* Reference to the view functions via $this->view
abstract class Core_Controller {
    protected $view;
    protected $model;

    function __construct(DependencyContainer $dependencyContainer){
        $this->view = new Core_View();
        //$this->view = $dependencyContainer->get(view);


    public function load($model){
        //load model
        //this part under construction and un-tested
        $this->$model = new $model;



user controller

 * Example Controller
class User_Controller extends Core_Controller {

    // domain.com/user/id-53463463
    function profile($userId)
        //GET data from a Model
        $profileData = $this->model->getProfile($userId);

        $this->view->load('userProfile', $profileData);

    // domain.com/user/friends/page-14
    function friends()
        //GET data from a Model
        $friendsData = $this->model->getFriends();

        $this->view->load('userFriends', $friendsData);
share|improve this question

3 Answers 3

up vote 1 down vote accepted

For me, I developed a separate object that handles all template display methods. This is good because you can then ensure that all the resources you need to display your UI is contained in one object. It looks like you've isolated this in Core_View.

Then, when an AJAX call is made, simply detect that it is an AJAX call. This can be done by either making the AJAX call through an AJAX object, which then references other objects, or you can take an easy approach and simply set an extra POST or GET field which indicates an AJAX call.

Once you've detected if it's an AJAX call, define a constant in your MVC such as AJAX_REQUEST. Then, in your template/UI object, you can specify that if it's an AJAX call, only output your response text. If it isn't, proceed with including your template files.

For me, I send it through an AJAX object. That way I don't have to worry about making a single output work for both cases. When it's ready to send a response, I just do something to the manner of print( json_encode( ...[Response]... ) ).

share|improve this answer

well, it would all start with normal request which would load the initial page. there are many options as to handle this but let's say that you start with /users/friends page which would list all your friends. then each of the friends should have link to specific friend's profile -- now this is the moment where ajax could kick in and you could ajaxify links to your friend profiles - this means that instead of normal you would instead use let's say jQuery and setup click handler in a such way that

$("a").click(function(){$.post($(this).attr("href"), null, function(data){$("#content").html(data);}});

this would use "href", and upon click would make post request to your backend. at backend, if you see that it's post, then you would just return the content for that particular friend. alternatively, if you have get request, you return all - header - content - footer.

if you use technique above, make sure to properly handle the data you receive. e.g. if there are further actions that should be done via ajax, make sure to "ajaxify" the data you get back. e.g. after updating html of the content, again apply the $("a").click routine.

this is just trivial example, to kick you off, but there are many more sophisticated ways of doing that. if you have time, I suggest reading some of agiletoolkit.org, it has nice mvc + ajax support.

share|improve this answer

You will need to use a different view. Maybe something like:

funciton friends() {
    $this->view = new Ajax_Request_View();
share|improve this answer

Your Answer


