PHP视图与控制器相关

时间:2012-08-18 18:07:34

标签: php templates yii yii-routing

我想实现连接到MVC等任何特定视图的控制器。我使用PHP中提供的任何框架

所以,我需要一些指导和建议。

我有一些控制器和视图。对于我的观点,我只想输出我的数据。 我现在关注的是我的控制器中的函数(如create())如何获取用户在$_POST['params']中输入数据的所有views/create.php,并在create()中创建新模型控制器的功能。

所以,现在,我想以这种方式做,我将在我的控制器文件夹中创建 MyViews 类。目的是加载特定视图并将所有$_POST参数放入对象中。然后, Users_controllers 等每个控制器都会创建 MyViews 。在 Users_controllers 的功能中,例如create()destroy(),我可能会使用 MyViews 中的函数来加载特定视图以加载对象。< / p>

我找到了加载视图的来源

<?php
class MyView {
protected $template_dir = 'templates/';
protected $vars = array();
public function __construct($template_dir = null) {
    if ($template_dir !== null) {
        // Check here whether this directory really exists
        $this->template_dir = $template_dir;
    }
}
public function render($template_file) {
    if (file_exists($this->template_dir.$template_file)) {
        include $this->template_dir.$template_file;
    } else {
        throw new Exception('no template file ' . $template_file . ' present in     directory ' . $this->template_dir);
    }
}
public function __set($name, $value) {
    $this->vars[$name] = $value;
}
public function __get($name) {
    return $this->vars[$name];
}
} ?>
嗯,我不知道如何检测_POST参数

    if(isset($_POST['Post']))
    {
        $model->attributes=$_POST['Post'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

这是我观察到的Yii框架。在加载特定视图后,如何检测params是$_POST还是$_GET

存档我的任务的任何指导和建议?

1 个答案:

答案 0 :(得分:1)

  

无声提问
您有一个主要问题:您表达意味着什么的能力非常有限。您提出的问题实际上与您的问题无关。

根据我收集的内容,您需要检测用户是否发出了POSTGET请求。直接检测它你可以检查$_SERVER['REQUEST_METHOD'],但用控制器检查它可能会非常麻烦。最终会有很多控制器的方法,这些方法根据请求方法表现不同。

由于您没有使用任何流行的框架,因此建议您将此决策委派给路由机制。

在我看来,处理此问题的一个很好的方法是使用请求方法为控制器的方法名称添加前缀:postLogin()getArticles()等。您可以找到一些额外的示例{{3} }。如果有POST个请求,则$_POST数组中包含某些内容。

  

<子>   调用“视图”的实际上是模板。如果您阅读here,您会注意到,那里的代码实际上是MyView的改进版本。视图不是模板。视图是包含表示逻辑和操作多个模板的实例。

P.S。如果您正在探索与PHP相关的MVC和MVC模式,您可能会发现this article很有用。