[英]PHP views relate controllers
I would like to implement controllers that connect to any specific views like MVC does. 我想实现与MVC一样连接到任何特定视图的控制器。 I'm not using any framework that provided in PHP.
我没有使用PHP提供的任何框架。
So, I need some guide and advice on doing it. 因此,我需要一些指导和建议。
I have some controllers and views. 我有一些控制器和视图。 For my views,i would like to just output my data only.
对于我的观点,我只想输出我的数据。 My concern now is how my function (like
create()
) in controllers, can get all the $_POST['params']
that users input data in my views/create.php
, and create a new Model in the create()
controllers's function. 现在,我要关心的是控制器中的函数(如
create()
)如何获取用户在我的views/create.php
输入数据的所有$_POST['params']
,并在create()
控制器的内部create()
新模型功能。
So,right now, i'm thinking to do in this way, I will create MyViews class in my controllers folder. 因此,现在,我正在考虑以这种方式进行操作,我将在控制器文件夹中创建MyViews类。 The purpose is loading the specific views and get all the
$_POST
params into an object. 目的是加载特定的视图,并将所有
$_POST
参数放入一个对象中。 Then, every controllers like Users_controllers , will create MyViews . 然后,每个类似Users_controllers的控制器都将创建MyViews 。 In the function of Users_controllers , like
create()
, destroy()
, I might use the function in MyViews to load specific views to load the object. 在Users_controllers函数中,例如
create()
, destroy()
,我可能会在MyViews中使用该函数来加载特定的视图以加载对象。
I found a source that load views 我找到了加载视图的源
<?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];
}
} ?>
hmm,I have no idea How I can detect the _POST params 嗯,我不知道如何检测_POST参数
if(isset($_POST['Post']))
{
$model->attributes=$_POST['Post'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
this is the Yii framework I observed. 这是我观察到的Yii框架。 How could I detect params whether is
$_POST
or $_GET
after load a specific views. 加载特定视图后,如何检测参数是
$_POST
还是$_GET
。
Any guidance and advice to archive my tasks? 有任何指导和建议来存档我的任务吗?
Unrelared to question
不用提问
You have one major problem: your ability to express what mean is extremely limited.您有一个主要问题:表达意图的能力非常有限。 The question, which you asked, was actually unrelated to your problem.
您提出的问题实际上与您的问题无关。
From what I gather, you need to detect of user made a POST
or GET
request. 根据我的收集,您需要检测用户发出的
POST
或GET
请求。 Do detect it directly you can check $_SERVER['REQUEST_METHOD']
, but checking it withing controller might be quite bothersome. 要直接检测它,可以检查
$_SERVER['REQUEST_METHOD']
,但是使用控制器检查它可能会很麻烦。 You will end up with a lot of controller's methods which behave differently based on request method. 您将最终得到许多控制器的方法,这些方法的行为根据请求方法而有所不同。
Since you are not using any of popular frameworks, is would recommend for you to instead delegate this decision to the routing mechanism. 由于您没有使用任何流行的框架,因此建议您将这个决定委托给路由机制。
A pretty good way to handle this, in my opinion, is to prefix the controller's method names with the request method: postLogin()
, getArticles()
etc. You can find few additional example here . 在我看来,一种很好的处理方法是在控制器的方法名称前添加请求方法:
postLogin()
, getArticles()
等。您可以在此处找到一些其他示例。 If there is a POST
request, it will have something in $_POST
array. 如果有
POST
请求时,它会在什么$_POST
阵列。
What are calling "views" are actually templates.
所谓的“视图”实际上是模板。 If you read this article , you will notice, that the code there is actually an improved version of your
MyView
.如果阅读本文 ,您会注意到,该代码实际上是
MyView
的改进版本。 Views are not templates.视图不是模板。 Views are instances which contain presentation logic and manipulate multiple templates.
视图是包含表示逻辑并操纵多个模板的实例。
PS If you are exploring MVC and MVC-inspired patterns in relation to PHP, you might find this post useful. PS:如果您正在探索与PHP有关的MVC和MVC启发式模式,那么您可能会发现这篇文章很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.