2015-11-13 39 views
1

啓動一個新的Fat Free Framework(F3)項目並且存在hive($ f3)全局變量未在視圖文件中定義/可用的問題。根據文件,$ f3應該是全球可用的。試圖訪問視圖文件中的$ f3,我得到一個錯誤。所以,這裏是代碼:

在index.php文件:

$f3->route('GET /','SomeController->index'); 

在SomeController.php:

class SiteController 
{ 

    public function index($f3) 
    { 
     $f3->set('title','Index'); 
     View::instance()->render('indexView.php'); 
    } 

} 

在indexView.php

echo $f3->get('title'); 

輸出

Internal Server Error 

Fatal error: Call to a member function get() on a non-object 

我解決這個問題得到通過明確地傳遞$ F3的觀點,像這樣:

View::instance()->render('indexView.php', 'text/html', array('f3'=>$f3)); 

但是,你不應該有這樣做。根據該文檔:

string render (string $file [, string $mime = 'text/html' [, array $hive = NULL ]]) 
Note: 
If no data $hive is provided, the global F3 hive is used. 

回答

3

的蜂房內容被提取,HTML編碼和傳遞給視圖,但整個$f3實例不是。

所以運行時:

$f3->set('title','Index'); 
$f3->set('text','Hello'); 
View::instance()->render('indexView.php'); 

下列變量將可以從該視圖:

  • $title
  • $text

$f3不會。

1

一般來說,你想保持你的視圖變量劃分,但這取決於你對MVC結構的意見。

但是,如果您確實需要訪問視圖內的$ f3 HIVE,只需在視圖頂部添加即可。

global $f3; 

// now you can use the HIVE 
echo $f3->get('myVar');