我真的很想了解兩個函數的最佳用法。Codeigniter重定向/加載視圖
控制器:(多的處理之後,可讓直接跳到創建會話)
$isLoggedin = array('user_name' => $row['username'] , 'user_type' => 'user' , 'is_loggedin' => TRUE);
$this->session->set_user($isLoggedin);
redirect('controller/index');
在控制器
,
public function __construct()
{
parent::__construct();
//should i check for the session here?
}
function index(){
// or should i check here?
}
在登錄控制器,在創建會話之後,我通過它到主控制器,在訪問主頁之前,我想驗證是否表示創建的會話= session['is_loggedin'] == TRUE
=登錄。
我的困惑是,因爲每次調用控制器時,構造函數都會正確加載。如果我錯了,請糾正我。所以在登錄控制器的重定向,我不應該調用索引?
我應該只是在那裏調用控制器,然後讓構造函數處理,然後在構造函數中,像這樣。
public function __construct(){
parent::__construct();
if($session['is_loggedin'] == TRUE)
$this->load->view('homepage');
else
redirect('Login');
}
所以我想我不會再需要索引了嗎?以這種方式調用控制器可以嗎?在重定向中,我也必須將$ session從控制器傳遞給控制器嗎?
在codeigniter中,構造函數的使用主要是加載可以在類的其他函數中使用的通用庫和輔助函數。當沒有提到特定功能時,索引函數作爲默認函數工作。 – user1906399 2014-10-05 07:09:50