Create administrator in cakephp

2

Good morning, I have a problem, the administrator user thinks, 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 goes well:

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 I'm asking for it from the main page and that's not what I want, if my main page is Index , I want it ask at 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 enrute to see if with that I did what I mentioned in www ... / admin but that does not do it is anda.

Router::connect('/admin', array('controller' => 'Users', 'action' => 'display', 'Users/login'));

that's how it works, but if you document AppController staying 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 comes out this:

Ps this is the index as it should be, now if I go to www ... / admin Now siven in admin pulls that out, and even though it pulls errors I get the layout default 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 clients

    
asked by Andrés Vélez 27.04.2018 в 17:14
source

1 answer

1

You can achieve what you want with the prefixes.

To be able to have an administrator section you must first declare a prefix in your file app/Config/core.php we search / create this line with your prefixes as the second parameter

Configure::write('Routing.prefixes', array('admin'));

In this way we can declare the function prefijo_accion() in any controller, for example if we want to have the route link we declare the function admin_login in this way

UsersController.php
<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {
    // Resto del código ...
    public function admin_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'));
            }
        }
    }
    // Resto del código ...
}

For the view we must create the file with the prefix admin, 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 administrator view you can create a file in the Layouts folder, say app/View/Layouts/admin.ctp , and in your AppController.php in your function beforeFilter tell it that if admin occupies the layout that you want, like this:

<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
    // Resto del código ...
    public function beforeFilter() {
        // Resto del código ...
        if (!empty($this->request->params['prefix'])  && $this->request->params['prefix'] == 'admin') {
            $this->layout = 'admin';
        }
        // Resto del código ...
    }
    // Resto del código ...
}

The logic of authentication and authorization is the same, you would only put it inside the functions with the prefix admin admin_función .

If you want to declare a specific route for your prefix you have to pass the option in the path parameter, for example if we want the login to be in link , you have to do it in the following way in your file routes.php

Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
    
answered by 27.04.2018 / 22:39
source