I have created the Simple Authentication that is described in The Cookbook for CakePHP 2.4. I have read here that a lot of had the same problem, that cakephp persists to redirect to login page when going to the homepage. But the solution just to add
$this->Auth->allow('display')
in my PagesController doesnt work. But it persists to redirect to the login page. All other Views can be accessed without being logged in and that is the strange thing.
Here is the code: AppController:
class AppController extends Controller {
public $components = array(
'DebugKit.Toolbar',
'Crud.Crud' => array(
'actions' => array(
// The controller action 'index' will map to the IndexCrudAction
'index' => 'Crud.Index',
// The controller action 'add' will map to the AddCrudAction
'add' => 'Crud.Add',
// The controller action 'edit' will map to the EditCrudAction
'edit' => 'Crud.edit',
// The controller action 'view' will map to the ViewCrudAction
'view' => 'Crud.View'
)
),
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'Pages',
'action' => 'display'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authorize' => array('Controller')
)
);
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index', 'view');
}
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
//Loading Crud Plugin
use CrudControllerTrait;
PagesController:
public function display() {
$path = func_get_args();
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count - 1]);
}
$this->set(compact('page', 'subpage', 'title_for_layout'));
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('display');
}
Just like said, the "$this->Auth->allow method seems to work for all other views but not for the homepage.
Edit: Problem solved! I've commented my "this->requestAction" out and added 'display' to the list of allowed methods and then it didnt redirect anymore! Thank you for your help! You also need to allow all included methods from your requested actions to be allowed!