【发布时间】:2013-01-23 08:46:08
【问题描述】:
我正处于使用 PHP 学习模型、视图、控制器框架的早期阶段。感谢here 找到的教程,我掌握了窍门。据我了解,控制器将调用模型,以便可以运行适当的代码,以便正确呈现可查看的内容。
问题在于,根据那个特定的教程,我似乎必须为每个页面创建一个全新的文件,只是为了调用模型才能使数据可见。
例如,在我的 controller.php 文件中,我有:
require_once("model.php");
class Controller
{
public $file;
public $league;
public function __construct($file, $league)
{
$this->file = $file;
$this->league = $league;
}
public function invoke()
{
// Everything that needs to be done in PHP will be done here so that once
// whatever file is included, it will just fit in fine and echo out perfectly
require_once($this->file.".php");
}
}
在我的 model.php 文件中,我有:
class Model
{
private $file;
private $league;
public function __construct($file, $league)
{
$this->file = $file;
$this->league = $league;
}
More code. You get the idea...
}
在我的 teams.php 文件中,我有:
$controller = new Controller("teams", "nba");
$controller->invoke();
但是,由于teams.php 必须调用模型并且最终需要require_once("teams.php"),所以我必须为视图创建一个全新的文件。因此,现在显示将在teams_display.php 上显示。无论如何,mod rewrite 是否可以帮助促进一个框架,使所有页面都发生在 index.php 文件中,同时给用户一种他们不是的错觉?
【问题讨论】:
-
你的前端控制器在哪里。这部分通常将处理请求的控制器和进行渲染的模板放在一起。控制器调用模型并将其中的数据传递到模板中。让一个控制器处理多个 URL 是一种从 URL 到控制器进行正确路由的方法。
-
哦。前控制器对我来说是新闻。我是 MVC 的初学者。直到几天前,我一直在对所有事情使用过程编程。你能给我看一个前端控制器的例子以及它是如何工作的吗?
-
我建议您不要尝试创建自己的 MVC 框架,而是抓住现有框架之一并进行探索。他们也有很多教程,而且代码质量比任何“构建自己的 MVC 框架”教程都要好得多。至少它们可以作为事物运作方式的示范。
-
那么,像 CakePHP 或 CodeIgniter 这样的东西?
-
我在考虑 Symfony 2.1 或 Zend Framework 2。
标签: php model-view-controller mod-rewrite