title: How to handle AJAX request in my PHP MVC?
body: <p>I am in the process of learning the MVC pattern and building my own lightweight one in PHP  </p>
  
  <p>Below is a basic example of what I have right now.  </p>
  
  <p>I am a little confused on how I should handle AJAX requests/responses though.  </p>
  
  <p>In my example user controller below, If I went to <code>www.domain.com/user/friends/page-14</code> in the browser, it would create a <code>User object</code> and call the <code>friends method</code> of that object</p>
  
  <p>The <code>friends method</code> would then get the data needed for the content portion of my page.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?  </p>
  
  <p>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</p>
  
  <pre><code>/**
  * Extend this class with your Controllers
  * Reference to the model wrapper / loader functions via $this-&gt;model
  * Reference to the view functions via $this-&gt;view
  */
  abstract class Core_Controller {
      protected $view;
      protected $model;
  
      function __construct(DependencyContainer $dependencyContainer){
          $this-&gt;view = new Core_View();
          //$this-&gt;view = $dependencyContainer-&gt;get(view);
  
  
      }
  
      public function load($model){
          //load model
          //this part under construction and un-tested
          $this-&gt;$model = new $model;
  
      }
  
  }
  </code></pre>
  
  <p>user controller</p>
  
  <pre><code>/**
   * Example Controller
   */
  class User_Controller extends Core_Controller {
  
      // domain.com/user/id-53463463
      function profile($userId)
      {
          //GET data from a Model
          $profileData = $this-&gt;model-&gt;getProfile($userId);
  
          $this-&gt;view-&gt;load('userProfile', $profileData);
      }
  
      // domain.com/user/friends/page-14
      function friends()
      {
          //GET data from a Model
          $friendsData = $this-&gt;model-&gt;getFriends();
  
          $this-&gt;view-&gt;load('userFriends', $friendsData);
      }
  }
  </code></pre>