title: CORS with Laravel 4
body: <p>I am writing an API, and using Laravel 4 to achieve that. My api is at a different domain. lets assume that it is: <code>http://api-example.com/</code></p>
  
  <p>And when i try to make ajax requests via Backbone to my api from my web-app (i.e <code>mydomain.com</code>) with basic authentication, it sometimes works just fine, but sometimes it doesn't. I am trying to figure out why. Below are my <code>App::before</code> filter and <code>App::after</code> filter. </p>
  
  <pre><code>App::before(function($request)
  {
      if($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
          $statusCode = 204;
  
          $headers = [
              'Access-Control-Allow-Origin'      =&gt; 'http://mydomain.com',
              'Allow'                            =&gt; 'GET, POST, OPTIONS',
              'Access-Control-Allow-Headers'     =&gt; 'Origin, Content-Type, Accept, Authorization, X-Requested-With',
              'Access-Control-Allow-Credentials' =&gt; 'true'
          ];
  
          return Response::make(null, $statusCode, $headers);
      }
  });
  </code></pre>
  
  <p>And my after filter:</p>
  
  <pre><code>App::after(function($request, $response)
  {
      $response-&gt;headers-&gt;set('Access-Control-Allow-Origin', 'http://mydomain.com');
      $response-&gt;headers-&gt;set('Allow', 'GET, POST, OPTIONS');
      $response-&gt;headers-&gt;set('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Requested-With');
      $response-&gt;headers-&gt;set('Access-Control-Allow-Credentials', 'true');
      return $response;
  });
  </code></pre>
  
  <p>The thing is when i try to make a post request to <code>/login</code> with the credentials, API checks the db and gets the API key for the user. This is just working fine. But when i try to make a POST request to <code>/users</code> chrome just gives me following error:</p>
  
  <pre><code>XMLHttpRequest cannot load http://api-example.com/users. Origin http://mydomain.com is not allowed by Access-Control-Allow-Origin.
  </code></pre>
  
  <p>I tried everything, such as setting <code>Access-Control-Allow-Origin</code> to <code>'*'</code> everything i could be able to find from internet. But nothing worked so far. I don't know what i should do.</p>