I\'m struggling with the concept of creating an admin section in CakePHP-project. (version 2.3.5)
I have uncommented the line in Config/core.php:
Configu
I guess you could set a loginAction in AppController and then inside this action you could do:
$this->redirect(array('controller'=>'someController','action'=>'someAction','admin'=>true));
I have another solution. Admin panel for website has another graphics, javasctipt etc, so I do in my app folders:
app_name
+Model
+Plugin
+admin
+front
Yes admin is in another folder, so I can for example set another cookie to login in admin area only plugins and model are the same. This folder structure needs some configuration in bootstrap.php:
App::build(array(
'Plugin' => array(ROOT . DS . 'Plugin' . DS),
'Model' => array(ROOT . DS . 'Model' . DS),
'Model/Behavior' => array(ROOT . DS . 'Model' . DS . 'Behavior' . DS),
));
Maybe is crazy for you but i prefer this solution that use admin routes.
try this
public $components = array(
'Auth' => array(
'autoRedirect' => false,
'loginRedirect' => array(
'admin' => true,
'controller' => 'dashboard',
'action' => 'index',
),
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'admin' => false,
'plugin' => false,
),
),
);
// Now the before Filter which tells it its okay to go to index/view/or display actions.
public function beforeFilter() {
// Allow public views
$this->Auth->allow('index', 'view', 'display');
}
for the user login you'd do something like this:
public function login() {
$this->set('title_for_layout', 'User Sign In');
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Username or password is incorrect', 'flash_bad');
}
}
}
any methods you want to access you'd do public function admin_index, admin_view admin_settings, etc. so that in the controller of widgets the route would be /admin/widgets/index /admin/widgets/index and so forth and so on. The trick to allowing other pages to be rendered without auth is just putting the $this->Auth->allow in the beforeFilter.
Try This
Your config/routes.php
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard' ));
Appcontroller
class AppController extends Controller {
public $components = array(
'Acl',
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array(
'username' => 'user_name',
'password' => 'password'
)
)
),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'users', 'action' => 'mysettings'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You don\'t have access here.',
/*
'loginAction' => array('controller' => 'users', 'action' => 'forgot_password'),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'forgot_password'),
'authError' => 'You don\'t have access here.',
*/
),
);
Usercontroller
class UsersController extends AppController {
/**
* Components
*
* @var array
*/
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login','logout');
}
}