Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove auth from the pages controller in CakePHP?

I'm using CakePHP's Auth component and it's in my app_controller.php.

Now I want to allow specific views from the pages controller. How do I do that?

like image 898
baker Avatar asked Dec 18 '09 08:12

baker


3 Answers

Copy the pages_controller.php file in cake/libs/controllers to your app/controllers/ dir. Then you can modify it to do anything you want. With the auth component, the typical way to allow specific access is like this:

class PagesController extends AppController {
 ...
 function beforeFilter() {
  $this->Auth->allow( 'action1', 'allowedAction2' );
 }
 ...

I recommend highly copying the file to your controllers dir, rather than editing it in place, because it will make upgrading cake much easier, and less likely that you accidentally overwrite some stuff.

like image 191
Travis Leleu Avatar answered Sep 28 '22 01:09

Travis Leleu


You could add the following to your app_controller.

function beforeFilter() {
  if ($this->params['controller'] == 'pages') {
    $this->Auth->allow('*'); // or ('page1', 'page2', ..., 'pageN')
  }
}

Then you don't have to make a copy the pages controller.

like image 38
Lawrence Barsanti Avatar answered Sep 28 '22 00:09

Lawrence Barsanti


I haven't tried the other ways but this is also the right way to allow access to all those static pages as display is that common action. In app_controller:

//for all actions    
$this->Auth->allow(array('controller' => 'pages', 'action' => 'display'));

//for particular actions
$this->Auth->allow(array('controller' => 'pages', 'action' => 'display', 'home'));
$this->Auth->allow(array('controller' => 'pages', 'action' => 'display', 'aboutus'));
like image 26
rajesh_kw Avatar answered Sep 27 '22 23:09

rajesh_kw