2013-03-13 54 views
0

Iam新cakephp和iam使用身份驗證我的登錄utiities ...我想重定向到登錄頁面incase我的會話experies爲我所有的行爲...我寫代碼像\如何重定向到登錄頁面是會話experies

public function index() {   
    if(!$this->Session->read('username')) 
     $this->logout(); 
    $this->set('users', $this->paginate());   
} 

但我所有的行動,如加(),編輯()......每次我需要檢查的會話變量...如果我寫這樣

public function __construct() 
{ 
    if(!$this->Session->read('username')) 
     $this->logout(); 
} 
在__construct條件

它給我錯誤像

Error: Call to a member function read() on a non-object 

任何人都可以建議我

+0

嘗試[認證組件](http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html) – 2013-03-13 06:42:07

回答

0

嘗試

if (!$this->Session->valid()) { 
    $this->logout(); 
} 
+0

不能......它給我「錯誤:調用成員函數有效()在非對象\t 「 – Gautam3164 2013-03-13 06:18:04

+0

怎麼樣session-> check()? – Ares 2013-03-13 06:20:15

+0

同樣的錯誤:調用非對象的成員函數check() – Gautam3164 2013-03-13 06:21:56

0

貌似你沒有包括SessionComponent。

試着在你的AppController先加

$components = array('Session');

然後檢查文檔的詳細信息:http://api.cakephp.org/2.3/class-SessionComponent.html

+0

Yah我補充說,即使 – Gautam3164 2013-03-13 07:40:02

+0

你可以發佈完整的控制器代碼嗎?和你的AppController的代碼? – nahri 2013-03-15 12:09:37

+0

也嘗試從Cookbook重建這兩個示例。 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html和http://book.cakephp.org/2.0/en/tutorials-and-examples/簡單-ACL控制的應用程序/簡單-ACL-受控application.html。如果你必須在_EVERY_控制器的_EVERY_動作中檢查你的會話,那麼你做錯了什麼。 Cake已經在AuthComponent中提供了這個功能。 – nahri 2013-03-15 12:11:29

0

使用AuthComponent如果會話到期和用戶試圖訪問受保護的頁面,他將被自動重定向到登錄頁面。

相關問題