FuelPHP基础,在视图中使用模型结果
我对使用FuelPHP1.7有点困惑 控制器FuelPHP基础,在视图中使用模型结果,php,templates,fuelphp,database-abstraction,Php,Templates,Fuelphp,Database Abstraction,我对使用FuelPHP1.7有点困惑 控制器 class Controller_Website extends Controller { public function action_index() { // http://fuelphp.com/docs/general/views.html $data = Website::get_results(); //var_dump($data) // (data is found
class Controller_Website extends Controller
{
public function action_index()
{
// http://fuelphp.com/docs/general/views.html
$data = Website::get_results();
//var_dump($data) // (data is found here);
$views = array();
$views['head'] = View::forge('common/head', $data);
$views['header'] = View::forge('common/header', $data);
$views['sidebar'] = View::forge('common/sidebar', $data);
$views['content'] = View::forge('common/content', $data);
$views['footer'] = View::forge('common/footer', $data);
// return the rendered HTML to the Request
return View::forge('website', $views)->render();
}
}
模型
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->execute();
return $result;
}
}
现在一切都好。在控制器中查询并找到数据。我试图实现的是使用我的
(嵌套)视图
<html>
<head>
<?php echo $head; ?>
</head>
<body>
<header>
<div class="container">
<?php echo $header; ?>
</div>
</header>
<div class="row">
<div class="container">
<div class="col-md-4">
<?php echo $sidebar; ?>
</div>
<div class="col-md-8">
<?php echo $content; ?>
</div>
</div>
</div>
<footer>
<div class="container">
<?php echo $footer; ?>
</div>
</footer>
</body>
</html>
头部视图(嵌套):
内容视图(嵌套):
等等
本例中视图中的变量不可用,因为它们未在控制器中显式设置。它们是否必须显式设置,或者是否也可以传递数据对象?如果是,如何以正确的方式访问此对象数据?FuelPHP在这里缺少好的例子,我现在被卡住了
我该怎么做 视图数据从索引数组转换为名为的视图变量。因此:
View::forge('something', array('param' => 'value'));
将对应于以下视图:
<h1><?=$param?></h1>
请注意,我首先使用
->to_array()
将结果对象转换为数组,然后使用reset()
获得第一个结果。我还添加了->as_assoc()
以确保获得数组结果,->as_object()
将为您提供一个stdClass实例。谢谢您让我继续!
View::forge('something', array('param' => 'value'));
<h1><?=$param?></h1>
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->as_assoc()
->execute()
->to_array();
return reset($result);
}
}