Good morning I have a problem I created the administrator user but the page does not work for me, here are the codes:
These codes are copied from the cake book 2.x
If I do this it works fine:
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Session' ,
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
The thing is that it asks me from the main page and that is not what I want, if my main page is Index , I want it to ask me in Idex/Admin
These are the codes that I have UsersController
<?php
// app/Controller/UsersController.php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
// app/Controller/UsersController.php
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->autoRedirect = false;
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('user', $this->User->findById($id));
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
}
}
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
} else {
$this->request->data = $this->User->findById($id);
unset($this->request->data['User']['password']);
}
}
public function delete($id = null) {
// Prior to 2.5 use
// $this->request->onlyAllow('post');
$this->request->allowMethod('post');
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->User->delete()) {
$this->Session->setFlash(__('User deleted'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('User was not deleted'));
return $this->redirect(array('action' => 'index'));
}
}
user model
<?php
App::uses('AppModel', 'Model');
class User extends AppModel {
public $validate = array(
'id' => array(
'rule' => 'notBlank'
),
'user' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A username is required'
)
),
'key' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A password is required'
)
),
'rol' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
),
'created' => array(
'rule' => 'notBlank'
)
);
}
login view
//app/View/Users/login.ctp
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
I even routed to see if with that I could do what I mentioned in www.../admin but that doesn't do it.
Router::connect('/admin', array('controller' => 'Users', 'action' => 'display', 'Users/login'));
This is how it works, but if the AppController document stays like this:
App::uses('Controller', 'Controller');
class AppController extends Controller {
// public $components = array(
// 'Session' ,
// 'Auth' => array(
// 'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
// 'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
// )
// );
//
// public function beforeFilter() {
// $this->Auth->allow('index', 'view');
// }
}
ps this comes out:
Ps this is the index as it should be, now if I go to www.../admin Now siven in admin pulls that up, and even though it brings up errors it brings up the default layout and I want to change it or at least not show me the menu (stripes on the right side) ps that menu is only for new customers
You can achieve what you want with prefixes.
In order to have an admin section you have to first declare a prefix in your file
app/Config/core.php
we look for/create this line with your prefixes as the second parameterIn this way we can declare the function
UsersController.phpprefijo_accion()
in any controller, for example if we want to have the route http://example.com/admin/users/login we declare the functionadmin_login
in this wayFor the view we must create the file with the admin prefix, for example admin_accion.ctp , for this case we create the file
app/View/Users/admin_login.ctp
,And if you need to declare a different layout for the admin view you can create a file in the Layouts folder, let's say
app/View/Layouts/admin.ctp
, and in your AppController.php in your functionbeforeFilter
tell it if it's admin to use the layout you want, like this:The authentication and authorization logic is the same, you would just put it inside functions prefixed with admin
admin_función
.If you want to declare a specific route for your prefix you have to pass the option in the route parameter, for example if we want the login to be at http://example.com/admin/login , you have to do it as follows: your file
routes.php