CakePHP Authentication

12 Sep 2008 . . Comments

#php #cakephp #authentication

I found some great tutorials on implementing ACL authentication in CakePHP. However, for the longest time I couldn’t get the actual example code to work. The way to authenticate everything in your application is to create an app_controller.php file in your app folder (see Preparing to Add Auth). In every other controller I’ve created in CakePHP, you follow this convention:

<?php

class FooBarsController extends AppController
{
  var $name = 'FooBars';
}

?>

I followed the same convention (didn’t read the docs very closely) and just got all kinds of errors. After digging a bit deeper, turns out AppController extends Controller…apparently it can’t extend itself :)

Hopefully this’ll save some time for someone, but the full controller for the example in the cookbook for the app_controller.php is this:

<?php

class AppController extends Controller {
  var $components = array('Auth', 'Acl');

  function beforeFilter() {
    //Configure AuthComponent
    $this->Auth->authorize = 'actions';
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
  }
}
?>