Auth Component的用户功能在多个路由的情况下不起作用

时间:2013-10-31 15:38:31

标签: php cakephp-2.0

我已将我的网站路由为管理员管理员。表示当我使用Auth组件时,我的会话被创建为Auth->Admin->id& Auth->Moderator->idAuth->User->id这样。

现在我尝试使用函数$this->Auth->user返回null为什么?

据我所知$this->Auth->user返回会话。

我知道$this->Session->reads('Auth.Admin')的传统方式,但我想使用Auth->用户函数为什么不能正常工作

2 个答案:

答案 0 :(得分:1)

请使用此功能获取用户会话数据。

public function login() {
 if ($this->request->is('post')) {
     if ($this->Auth->login()) {
    $this->redirect($this->Auth->redirect());
     } else {
    $this->Session->setFlash(__('Invalid username or password, try again'));
     }
 }
}

public function index() {
    pr($this->Auth->User());
}

答案 1 :(得分:0)

        auth_helper:<?php
        defined('BASEPATH') OR exit('No direct script access allowed');

        if ( ! function_exists('auth'))
        {
            function auth()
            {
                $CI =& get_instance();
                $CI->load->model('Auth_model');
                return $CI->Auth_model;
            }

            function Adminauth($admin_id) 
            {
                if($admin_id == false)
                {
                    redirect('login');
                }
            }
        }
    auth_model:
    <?php
    if (!defined('BASEPATH')) {
        exit('No direct script access allowed');
    }

    class Auth_model extends CI_Model {
        private $user = null;
        private $ci = null;

        public function __construct() {
            parent::__construct();
            $CI =& get_instance();
            $this->ci = $CI;
        }

        public function get($name, $default = null){
            if ($this->ci->session->has_userdata('auth.Adminuser_id')) {
                 $user_id = $this->ci->session->userdata('auth.Adminuser_id');
                 $this->user = $this->db->query("SELECT * FROM members WHERE u_id=? LIMIT 1", array($user_id))->row();
            }

            return !empty($this->user) ? $this->user->u_name : $default;
        }
        public function is_logged(){
            return $this->ci->session->has_userdata('auth.Adminuser_id');
        }
        public function login($user_id){
            return $this->ci->session->set_userdata('auth.Adminuser_id', $user_id);
        }
        public function logout(){
            return $this->ci->session->unset_userdata('auth.Adminuser_id');
        }
    }