title: Laravel 4: Responding to AJAX requests from controller
body: <p>I'm trying to generate ajax specific responses from my controllers by using the Request::ajax() method, which is working just fine. The only problem is that the way I have it set up right now isn't really a nice looking solution.</p>
  
  <p>My controller:</p>
  
  <pre><code>class HomeController extends BaseController {
  
  protected $layout = 'layouts/main';
  
  public function __construct()
  {
      $this-&gt;beforeFilter('auth');
  }
  
  public function getIndex()
  {
      $view = View::make('content.home.index');
      if(Request::ajax()) return $view; //For ajax calls we only want to return the content to be placed inside our container, without the layout
      $this-&gt;layout-&gt;menu = 'content.menu';
      $this-&gt;layout-&gt;content = $view;
  }
  
  }
  </code></pre>
  
  <p>So right now, for every method I define within my controllers I need to add the code snippet that checks for an AJAX request and returns a single view if the statement returns true.</p>
  
  <p>This leads to my question that is probably more PHP related than it is to the framework;</p>
  
  <p>Is there a way of executing my AJAX check on every method call, without actually placing it inside the method? Or is there some other solution to keep my code DRY?</p>
  
  <p>Thanks in advance!</p>
  
  <p>PS: This is my first post on stackoverflow, so feel free to correct me if I made any mistakes</p>