Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从控制器转到视图_Php_Model View Controller - Fatal编程技术网

Php 从控制器转到视图

Php 从控制器转到视图,php,model-view-controller,Php,Model View Controller,我正在努力创建自己的非常简单的MVC,我正在集思广益,寻找从控制器到视图的方法。这涉及到将变量从一个类发送到一个普通的旧PHP页面 我相信这之前已经被讨论过了,但我想看看人们能想出什么样的想法 //this file would be /controller/my_controller.php class My_Controller{ function someFunction(){ $var = 'Hello World'; //how do we get var to o

我正在努力创建自己的非常简单的MVC,我正在集思广益,寻找从控制器到视图的方法。这涉及到将变量从一个类发送到一个普通的旧PHP页面

我相信这之前已经被讨论过了,但我想看看人们能想出什么样的想法

//this file would be /controller/my_controller.php

class My_Controller{

   function someFunction(){

  $var = 'Hello World';
  //how do we get var to our view file in the document root?
  //cool_view.php

   }

}

某种哈希表是一种很好的方法。将变量作为关联数组返回,该数组将填补视图中的所有空白。

将变量作为属性存储在控制器对象中,然后在渲染时提取它们

class My_Controller {

    protected $locals = array();

    function index() {
        $this->locals['var'] = 'Hello World';
    }

    protected function render() {
        ob_start();
        extract($this->locals);
        include 'YOUR_VIEW_FILE.php';
        return ob_get_clean();
    }
}
您可以定义那些神奇的get和set方法,使其更漂亮

$this->var = 'test';

我也在开发自己的简单MVC,最简单的方法是

class My_Controller
{

   function someFunction() {
       $view_vars['name'] = 'John';
       $view = new View('template_filename.php', $view_vars);
   }

}
视图类

class View
{
   public function __construct($template, $vars) {
       include($template);
   }
}
template_filename.php

Hello, <?php echo $vars['name'];?>
你好,

我强烈建议您看看PHP专家

我为免费的PHP课程创建了自己的MVC,我正在为少数希望在PHP方面取得更好成绩的人进行课程

到目前为止,最好的方法是使用Command+Factory模式

例如

在每个控制器中:

class UserController implements ControllerCommand
{
    public function execute($action)
    {
        if ($action == 'login')
        {
            $data['view_file'] = 'views/home.tpl.php';
        }
        else if ($action == 'edit_profile')
        {
            $data['view_file'] = 'views/profile.tpl.php';
            $data['registration_status'] = $this->editProfile();
        }

        return $data;
    }
}
$data = ControllerCommandFactory::execute($action);
if (!is_null($data)) { extract($data); }
/* We know the view_file is safe, since we explicitly set it above. */
require $view_file;
<?php
class Controller
{
  public function someAction()
  {
    $this->view->something = 'somevalue'; 
  }
}
?>
从主前端控制器:

class UserController implements ControllerCommand
{
    public function execute($action)
    {
        if ($action == 'login')
        {
            $data['view_file'] = 'views/home.tpl.php';
        }
        else if ($action == 'edit_profile')
        {
            $data['view_file'] = 'views/profile.tpl.php';
            $data['registration_status'] = $this->editProfile();
        }

        return $data;
    }
}
$data = ControllerCommandFactory::execute($action);
if (!is_null($data)) { extract($data); }
/* We know the view_file is safe, since we explicitly set it above. */
require $view_file;
<?php
class Controller
{
  public function someAction()
  {
    $this->view->something = 'somevalue'; 
  }
}
?>
关键是每个Controllercommand类都有一个execute函数,该函数返回其视图和该视图的任何数据

对于完整的MVC,您可以通过向theodore[at]phpexperts.pro.发送电子邮件来访问开源应用程序。

我会检查它是如何完成视图渲染的

您可以在github上获得和的源代码——不幸的是,我没有发现当前的存储库(在svn中)那么容易浏览

基本上,视图变量包含在
视图
对象中(控制器可以访问该对象),然后在该对象中呈现模板(普通的旧php文档)。该方法允许模板访问
$this

可能是这样的:

<?php
class View
{
  public function render()
  {
    ob_start();
    include($this->_viewTemplate); //the included file can now access $this
    return ob_get_clean();
  }
}
?>

因此,在控制器中:

class UserController implements ControllerCommand
{
    public function execute($action)
    {
        if ($action == 'login')
        {
            $data['view_file'] = 'views/home.tpl.php';
        }
        else if ($action == 'edit_profile')
        {
            $data['view_file'] = 'views/profile.tpl.php';
            $data['registration_status'] = $this->editProfile();
        }

        return $data;
    }
}
$data = ControllerCommandFactory::execute($action);
if (!is_null($data)) { extract($data); }
/* We know the view_file is safe, since we explicitly set it above. */
require $view_file;
<?php
class Controller
{
  public function someAction()
  {
    $this->view->something = 'somevalue'; 
  }
}
?>

以及您的模板:

<p><?php echo $this->something;?></p>


在我看来,这种模式允许您在视图中具有很大的灵活性。

因此建议您只返回控制器中的变量。然后在视图中说$vars=newmyu controller();然后使用适当的函数。这确实是一个很好的简单解决方案。使用
提取时要非常小心,使用前要仔细阅读