启动一个新的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.
答案 0 :(得分:3)
hive内容被提取,HTML编码并传递给视图,但整个$f3
实例不是。
所以跑步时:
$f3->set('title','Index');
$f3->set('text','Hello');
View::instance()->render('indexView.php');
视图中将提供以下变量:
$title
$text
但$f3
不会。
答案 1 :(得分:1)
通常,您希望将视图变量划分为区分,但这取决于您对MVC结构的看法。
但是,如果您确实需要在视图中访问$ f3 HIVE,只需在视图顶部添加即可。
global $f3;
// now you can use the HIVE
echo $f3->get('myVar');