2015-04-06 38 views
0

我有kohana框架的神祕問題。Kohana控制器無法訪問會話變量

我控制器功能創建會話變量:

public function action_authorise() 
    { 
     session_start(); 
     $_SESSION["user"] = "superAdmin"; 
    } 

在同一個控制器的另一個功能,後來我嘗試訪問本賽季

public function action_getSession() 
    { 
     $this->template->test = $_SESSION["user"]; 
     $this->template->content = View::factory('admin/main'); 
    } 

的問題是,當我叫$測試變量在管理員/主視圖中,它返回空字符串,但如果我在admin/main視圖中隱式地調用$ _SESSION [「user」],它將返回「superAdmin」,因爲它應該如此。

任何人都可以在控制器中調用會話變量時看到錯誤嗎?謝謝

回答

0

這裏的問題是,您將變量測試傳遞給視圖template,它需要傳遞給視圖admin/main。爲此,您可以通過兩種方式,選擇你最喜歡哪一個:

// Create the view object 
$partial_view = View::factory('admin/main'); 
// Assign the session value to the partial view's scope as `test` 
$partial_view->test = $_SESSION["user"]; 
// Assign the partial view to the main template's scope as `content` 
$this->template->content = $partial_view; 

快捷語法:

$this->template->content = View::factory('admin/main', array(
    'test' => $_SESSION['user'], 
)); 
0

您傳遞test變量template視圖,但在嘗試訪問它admin/main視圖。在admin/main視圖中沒有test變量。這些是不同的看法。每個人都有自己的變數。

您應該設置testadmin/main觀點,如:

public function action_getSession() 
    { 
     $this->template->content = View::factory('admin/main') 
      ->set('test', $_SESSION["user"]); 
    } 

也有在Kohana中非常有用Session類。它負責框架內的會話業務。

看看user guide