可能重複:
echo user in view from sessions code igniter回波會話名未在每個控制器定義
我不想定義和用戶存儲在每一個控制器,然後把它傳遞給視圖。 這裏是我的控制器:
登錄控制器:
class LoginController extends CI_Controller {
function index(){
$new['main_content'] = 'loginView';
$this->load->view('loginTemplate/template', $new);
}
function verifyUser(){
//getting parameters from view
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$this->load->model('loginModel');
$query = $this->loginModel->validate($data);
if ($query){
//if the user c validated data variable is created becx we want to put username in session
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('sessionController/dashboard_area');
}else{
$this->index();
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
?>
我的控制,我已經保留的核心文件夾中,所以現在每個控制器現在擴展了該控制器。我覺得這個控制器可以定製,所以我可以在其中擴展了該控制器每個視圖頁面訪問用戶:
class MY_Controller extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
}
function dashboard_area(){
$data['main_content'] = 'dashboardView';
$this->load->view('dashboardTemplate/template', $data);
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page.';
redirect('loginController');
}
}
}
?>
這裏是我的簡單的一個控制器,擴展上述控制器: 在這裏,指數函數,我存儲用戶名,然後傳遞到我不想做的觀點:
class CategoryController extends MY_Controller {
function index(){
$data['main_content'] = 'categoryView';
$username= $this->session->userdata('username');
$data['username']=$username;
$this->load->view('dashboardTemplate/template',$data);
}
是不是這個問題[你以前問過](http://stackoverflow.com/questions/14336122/echo-user-in-view-from-sessions-code-igniter)? – AgentConundrum