Php MVC:如何将阵列从模型传递到视图?
Php MVC:如何将阵列从模型传递到视图?,php,model-view-controller,Php,Model View Controller,$task数组在模型中显示如下: Array ( [id] => 1 [0] => 1 [name] => Nicolas IV [2] => Nicolas IV [email] => breitenberg.dedrick@example.com [3] => breitenberg.dedrick@example.com ... 但是view task_show.php返回一个错误: Un
$task
数组在模型中显示如下:
Array (
[id] => 1
[0] => 1
[name] => Nicolas IV
[2] => Nicolas IV
[email] => breitenberg.dedrick@example.com
[3] => breitenberg.dedrick@example.com
...
但是view task_show.php返回一个错误:
Undefined variable: task
请告诉我我的代码有什么问题
控制器
<?php
class Index extends Controller{
public function __construct() {
parent::__construct();
$this->view->render('index/index');
require 'models/index_model.php';
}
public function task_show($id = false) {
$tasks = Index_Model::task_show($id);
require 'views/index/task_show.php';
}
}
事实上,我会扩展我的评论,因为我会留出一些建议: 此代码:
public function __construct() {
parent::__construct();
}
在你的模型里什么都不做。移除它。
这样:
$tasks = Index_Model::task_show($id);
您调用静态方法
public function task_show($id) {
事实并非如此
当你处理错误时,它会告诉你哪里出了问题,至少在某些情况下是这样的
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
事实上,我将扩大我的评论,因为我只提供了一些建议: 此代码:
public function __construct() {
parent::__construct();
}
在你的模型里什么都不做。移除它。
这样:
$tasks = Index_Model::task_show($id);
您调用静态方法
public function task_show($id) {
事实并非如此
当你处理错误时,它会告诉你哪里出了问题,至少在某些情况下是这样的
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
在控制器中调用该方法。将结果放入变量$someName中,然后在控制器中包含该view.php。实际上您已经完成了,但控制器中有$tasks,视图中有$task。要么更改
$tasks=Index\u Model::task\u show($id)代码>至$task=Index\u Model::task\u show($id)代码>或echo$task['name']
toecho$tasks['name']
并查看在控制器中调用该方法时发生了什么。将结果放入变量$someName中,然后在控制器中包含该view.php。实际上您已经完成了,但控制器中有$tasks,视图中有$task。要么更改$tasks=Index\u Model::task\u show($id)代码>至$task=Index\u Model::task\u show($id)代码>或echo$task['name']
toecho$tasks['name']代码>并查看发生了什么