title: How should one serve swagger from laravel?
body: <p>I'm attempting to use <a href="https://helloreverb.com/developers/swagger">swagger</a> with laravel to automatically document our RESTful API. The goal is to keep the swagger comments in the laravel controllers and then have swagger parse the comments and generate the associated .json/.php files. Ideally, I'm looking to have the swagger files be served by the laravel project so that everything is kept under the same hood and in sync.</p>
  
  <p>In order to accomplish this, I have created a <em>docs</em> directory in the root directory of my laravel project (same directory that <em>public</em> resides in). I've then added the following route to routes.php:</p>
  
  <pre><code>Route::get('docs/{page?}', function($page='index.php') {
      header('Access-Control-Allow-Origin: *');
      $parts = pathinfo($page);
      $path = $_SERVER["DOCUMENT_ROOT"] . "/../docs/$page";
      if ($parts['extension'] === 'php') {
          require($path);
      } else {
          return file_get_contents($path);
      }
  });
  </code></pre>
  
  <p>Using this method I am then able to point my swagger-ui website to <em>http://mydomain/docs</em> and the rest is magic.</p>
  
  <p>For all you laravel gurus out there, is this the best way to serve these swagger files? I tried putting the <em>docs</em> directory in <em>public</em> but this leads to a redirect loop.</p>
  
  <p>Another way to accomplish this is to create a virtual host in my web server config that points directly to these swagger files, but at this point I'd prefer not to have to make this extra configuration.</p>