title: CakePHP 2.1 - As a web application and REST service with Authentication
body: <p>I'm currently developing a CakePHP application which currently has form authentication. I would also like to open up this application for other applications to connect to via REST.</p>
  
  <p>I know that CakePHP would be able to do this using the </p>
  
  <pre><code>Router::mapResources() 
  </code></pre>
  
  <p>and </p>
  
  <pre><code>Router::parseExtensions() 
  </code></pre>
  
  <p>However, I'm unsure how to get this working with say Basic or Digest HTTP authentication.</p>
  
  <p>I've got the following in the AppController.php</p>
  
  <pre><code>public $components = array(
      'Session',
      'Auth' =&gt; array(
          'authenticate' =&gt; array(
              'Form'
          ),
          'loginAction' =&gt; array(
              'admin' =&gt; false,
              'controller' =&gt; 'users',
              'action' =&gt; 'login'
          ),
          'loginRedirect' =&gt; array(
              'controller' =&gt; 'users',
              'action' =&gt; 'home'
          )
      )
  );
  </code></pre>
  
  <p>If for the authenticate field, I had in 'Basic' for example - when logging into the web based version, I get an HTTP auth box and not the web based form.</p>
  
  <p>What is the best way of doing this? The only way I can think of at the moment is to create a separate ApiController and manually do authentication?</p>
  
  <p>Any advise would be awesome.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This is my revised code which is giving me the correct behavour - I'm pretty sure that there should be a better way to do this.</p>
  
  <pre><code>class AppController extends Controller {
  
      public $components = array(
          'Session',
          'RequestHandler',
          'Auth' =&gt; array(
              'loginAction' =&gt; array(
                  'admin' =&gt; false,
                  'controller' =&gt; 'users',
                  'action' =&gt; 'login'
              ),
              'loginRedirect' =&gt; array(
                  'controller' =&gt; 'users',
                  'action' =&gt; 'home'
              )
          )
      );
  
      public $helpers = array('Html', 'Form', 'Session');
  
      public function beforeFilter() {
          $header = $_SERVER['HTTP_AUTHORIZATION'];
          if($header) {
              $this-&gt;Auth-&gt;authenticate = array('Basic');
          }
      }
  
  }
  </code></pre>