J86*_*J86 2 php codeigniter http-method
我喜欢 MVC(非常喜欢),并且我正在尝试用当今所有主要的 Web 语言自学一个 MVC 架构框架。
我目前在使用 CodeIgniter 和 PHP。我在网上搜索了一种方法来使相同的函数对于 POST 和 GET 表现不同,但找不到任何东西。CodeIgniter 有这个功能吗?
如果您使用过 Ruby On Rails 或 ASP.NET MVC,您就会知道我在说什么,在它们的框架中,我们可以这样做:
[GET]
public ActionResult Edit(int Id)
{
// logic here for GET
}
[POST]
public ActionResult Edit(EntityX EX)
{
// logic here for POST
}
Run Code Online (Sandbox Code Playgroud)
我已经习惯了这一点,以至于我发现很难在没有这种有用能力的情况下如何获得相同的流畅功能。
我错过了什么吗?如何在 CodeIgniter 中实现相同的功能?
谢谢
我错过了什么吗?如何在 CodeIgniter 中实现相同的功能?
如果你想学习如何真正在 PHP 中处理 MVC,你可以从 Tom Butler 的文章中学习
CodeIgniter 实现了 Model-View-Presenter 模式,而不是 MVC(即使它这么说)。如果您想实现一个真正类似于 MVC 的应用程序,那么您就走错了路。
在MVP中:
注意:模型永远不应该是单一的 class。它的许多类。我将其称为“模型”,仅用于演示。
所以它看起来像:
class Presenter
{
public function __construct(Model $model, View $view)
{
$this->model = $model;
$this->view = $view;
}
public function indexAction()
{
$data = $this->model->fetchSomeData();
$this->view->setSomeData($data);
echo $this->view->render();
}
}
Run Code Online (Sandbox Code Playgroud)
在MVC中:
$_GET或$_POST例如,
class View
{
public function __construct(Model $model)
{
$this->model = $model;
}
public function render()
{
ob_start();
$vars = $this->model->fetchSomeStuff();
extract($vars);
require('/template.phtml');
return ob_get_clean();
}
}
class Controller
{
public function __construct(Model $model)
{
$this->model = $model;
}
public function indexAction()
{
$this->model->setVars($_POST); // or something like that
}
}
$model = new Model();
$view = new View($model);
$controller = new Controller($model);
$controller->indexAction();
echo $view->render();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9686 次 |
| 最近记录: |