Php 将变量从控制器传递到视图
我有一个问题,我一直在制作自己的MVC应用程序,但在模型和控制器之间传递变量似乎有问题。控制器的输出是一个包含一些json格式数据的单个变量,看起来很简单 控制器Php 将变量从控制器传递到视图,php,model-view-controller,Php,Model View Controller,我有一个问题,我一直在制作自己的MVC应用程序,但在模型和控制器之间传递变量似乎有问题。控制器的输出是一个包含一些json格式数据的单个变量,看起来很简单 控制器 <?php class controllerLib { function __construct() { $this->view = new view(); } public function getModel($model) {
<?php
class controllerLib
{
function __construct()
{
$this->view = new view();
}
public function getModel($model)
{
$modelName = $model."Model";
$this->model=new $modelName();
}
}
class controller extends controllerLib
{
function __construct()
{
parent::__construct();
}
public function addresses($arg = false)
{
echo'Addresses '.$arg.'<br />';
$this->view->render('addressesView');
$this->view->showAddresses = $this->model->getAddresses();
}
}
?>
看法
现在的问题是$this->showAddresses没有传递到view,并且我被卡住了。代码有各种问题:
$this->showAddresses
在构造函数时具有值function __construct()
{
parent::__construct();
require 'view/head.php';
$result = $this->showAddresses; // (NULL) The object is not created yet
require 'view/foot.php';
}
视图类:
public function factory($plik) // Old render($splik) method
{
return new $plik();
}
function __construct()
{
parent::__construct();
}
function render()
{
require 'view/head.php';
$result = $this->showAddresses; // Object is created and the field has a value
require 'view/foot.php';
}
AddressView类:
public function factory($plik) // Old render($splik) method
{
return new $plik();
}
function __construct()
{
parent::__construct();
}
function render()
{
require 'view/head.php';
$result = $this->showAddresses; // Object is created and the field has a value
require 'view/foot.php';
}
控制器类:
$view = $this->view->factory('addressesView');
$view->showAddresses = $this->model->getAddresses();
$view->render();
很多基本的东西都出了问题,我建议您再次阅读OOP和MVC模型。稍后我将尝试更正此代码。我建议您阅读有关Factory模式设计的内容。答案中的代码旨在减少更改的数量,但是您的视图类应该是一个独立的类,并且您的AddressView至少应该实现一个interfaceDone以及独立视图和interfece:)