从控制器到视图

发布于 2024-09-16 00:49:15 字数 353 浏览 6 评论 0原文

我正在致力于创建自己的非常简单的 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

   }

}

I am working on creating my own very simple MVC and I am brainstorming ways to go from the controller to the view. Which involves sending variables from a class to just a plain old PHP page.

I am sure that this has been covered before, but I wanted to see what kind of ideas people could come up with.

//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

   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一袭水袖舞倾城 2024-09-23 00:49:15

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

Some kind of hashtable is a good way to do that. Return your variables as association array which will fill all the gaps in your view.

挽清梦 2024-09-23 00:49:15

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

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';

Store your variables as a property in your controller object, then extract them when rendering

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();
    }
}

You can define those magic __get and __set methods to make it prettier

$this->var = 'test';
小霸王臭丫头 2024-09-23 00:49:15

我也在开发自己的简单 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 Savant http://phpsavant.com/docs/

I'm also developing my own simple MVC and the most simple way to do it is ...

class My_Controller
{

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

}

View class

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

template_filename.php

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

I highly recommend you to take a look at PHP Savant http://phpsavant.com/docs/

愁杀 2024-09-23 00:49:15

我会查看 Zend_View 以及如何它完成了视图渲染。

您可以获取 的源代码查看AbstractView - 不幸的是,我找不到易于浏览的当前存储库(在 svn 中)。

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

它会是这样的:

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

所以在你的控制器中:

<?php
class Controller
{
  public function someAction()
  {
    $this->view->something = 'somevalue'; 
  }
}
?>

和你的模板:

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

在我看来,这种模式可以让你在视图方面有很大的灵活性。

I'd checkout Zend_View and how it accomplished view rendering.

You can get the source of View and AbstractView on github - unfortunaly I don't find the current repository (in svn) that easy to browse.

Essentially the view variables are contained in a View object (which your controller would have access to), then the template (plain old php document) is rendered inside that object. That method allows the template access to $this.

It would be something like:

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

So in your controller:

<?php
class Controller
{
  public function someAction()
  {
    $this->view->something = 'somevalue'; 
  }
}
?>

And your template:

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

In my opinion this pattern allows you much flexibility with the view.

我为一些想要提高 PHP 水平的人开设的免费 PHP 课程创建了自己的 MVC。

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

例如

interface ControllerCommand
{
    public function execute($action);
}

,在每个控制器中:

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;

要点是每个 Controllercommand 类都有一个执行函数,并返回其视图和该视图的任何数据。

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

I created my own MVC for the free PHP course I'm conducting for a handful of people wanting to get better at PHP.

By far the best way to do this is to use the Command + Factory pattern.

E.g.

interface ControllerCommand
{
    public function execute($action);
}

In each controller:

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;
    }
}

From your main front controller:

$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;

The point is that every Controllercommand class has an execute function and that returns its view and any data for that view.

For the complete MVC, you can access the open source app by emailing me at theodore[at]phpexperts.pro.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文