CakePHP admin section routing and redirecting

后端 未结 4 1760
醉梦人生
醉梦人生 2021-02-20 09:49

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         


        
4条回答
  •  遥遥无期
    2021-02-20 10:40

    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.

提交回复
热议问题