【发布时间】:2013-09-27 00:42:02
【问题描述】:
我想实现像 MVC 那样连接到任何特定视图的控制器。我不使用 PHP 中提供的任何框架。
所以,我需要一些指导和建议。
我有一些控制器和视图。对于我的观点,我只想输出我的数据。
我现在关心的是我在控制器中的函数(如create())如何获取用户在我的views/create.php 中输入数据的所有$_POST['params'],并在create() 控制器的函数中创建一个新模型。
所以,现在,我正在考虑这样做,我将在我的控制器文件夹中创建 MyViews 类。目的是加载特定视图并将所有$_POST 参数放入对象中。然后,像 Users_controllers 这样的每个控制器都会创建 MyViews。在Users_controllers的函数中,比如create()、destroy(),我可能会使用MyViews中的函数来加载特定的视图来加载对象。
我找到了一个加载视图的源
<?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 框架。加载特定视图后如何检测参数是$_POST 还是$_GET。
对归档我的任务有任何指导和建议吗?
【问题讨论】:
-
试试
if(count($_POST) > 0) {。
标签: php templates yii yii-routing