我正在PHP中创建一个基本的MVC结构化CMS,以此来学习MVC的工作原理(因此我没有使用真正的预构建引擎)。我有一个基本版本,它的结构与这里的教程非常相似。然而,我希望视图能够自动加载,而不需要模板类。如果强烈建议这样做,我会坚持模板的概念(如果有人能解释为什么它如此必要,我将不胜感激。)无论如何,下面是我的路由器类,我已经修改了它,可以沿着控制器自动加载视图文件。
public function loader() {
/*** check the route ***/
$this->getPath();
/*** if the file is not there diaf ***/
if (is_readable($this->controller_path) == false) {
$this->controller_path = $this->path.'/controller/error404.php';
$this->action_path = $this->path.'/view/error404.php';
}
/*** include the path files ***/
include $this->controller_path;
include $this->action_path;
/*** a new controller class instance ***/
$class = $this->controller . 'Controller';
$controller = new $class($this->registry);
/*** check if the action is callable ***/
if (is_callable(array($controller, $this->action)) == false) {
$action = 'index';
} else {
$action = $this->action;
}
$controller->$action();
}
/**
*
* @get the controller
*
* @access private
*
* @return void
*
*/
private function getPath() {
/*** get the route from the url ***/
$route = (empty($_GET['rt'])) ? '' : $_GET['rt'];
if (empty($route)) {
$route = 'index';
} else {
/*** get the parts of the route ***/
// mywebsite.com/controller/action
// mywebsite.com/blog/hello
$parts = explode('/', $route);
$this->controller = $parts[0];
if(isset( $parts[1])) {
$this->action = $parts[1];
}
}
if( ! $this->controller ) { $this->controller = 'index'; }
if( ! $this->action ) { $this->action = 'index'; }
/*** set the file path ***/
$this->controller_path = $this->path .'/controller/'. $this->controller . '.php';
$this->action_path = $this->path .'/view/'. $this->controller . '/'. $this->action . '.php';
}
这阻止了我的视图文件加载控制器给定的变量(教程网站对此有更好的演示),但在设置$this->registry->template->blog_heading = 'This is the blog Index';
时,视图不会加载它,因为绕过了template.class。基本上,我要问的是如何将template.class转换为加载函数?
对"视图只是一个愚蠢的模板"的常见误解主要是由RubyonRails和那些遵循其损坏的ORM模板适配器的人所延续的。我不能直接把他们实现的东西称为模型视图控制器。。。
视图应该处理MVC和MVC启发的设计模式模式中的表示逻辑。这使它们成为对象,能够处理多个模板。
根据您在web应用程序中使用的受MVC启发的模式(不可能用PHP实现经典MVC),您的视图要么从类似控制器的结构(MVP和MVVM模式)接收数据,要么能够直接从模型层请求信息(Model2MVC和HMVC模式)。我个人更喜欢从模型层获取数据的活动视图。
p.S.像$this->registry->template->blog_heading
这样的代码会让德米特流血。
p.p.S.关于如何实现纯php模板,请阅读本文。
我知道这对你现在没有太大帮助,但几个月前我也遇到了同样的问题。这是基于我构建的框架:
https://github.com/andyhmltn/Cherry-Framework-Blog-Example/
我不太确定它是在哪里或如何实现的,因为我已经有一段时间没有真正看过它了,但四处看看,加载控制器、设置变量然后加载视图的代码可能在库文件夹中。它允许你这样做:
/** Controller **/
class ExampleController extends Controller {
public function index() {
$helloworld = 'Hello world';
$this->set('hello_world', $helloworld);
#Renders view automatically
}
}
/** View **/
echo $hello_world;
在我自己开发的MVC中,加载视图的工作方式与您所拥有的类似,但与您链接到的示例相比要简单得多。(当我第一次决定学习MVC时,我看了那个例子,我记得它把我搞糊涂了
本质上,在您确定文件存在后,您只需要要求(是的,要求,我觉得在这种情况下,找不到文件是停止执行脚本的一个很好的理由)该文件。
所以。。这里有一个简单的控制器打开视图文件的例子,而不是整个模板类的事情(我希望我没有回避你的问题,也没有偏离基础太远)
<?php
class Pizza_Shop_Controller extends Base_Controller
{
public function index()
{
$data['users'] = array('bob','lisa','bertha');
$data['some_string'] = "Whoa I'm a string!";
$this->render_view('index',$data);
}
public function contact()
{
if($_POST)
{
Contact::process($_POST);
return $this->render_view('contact_success');
}
else
{
return $this->render_view('contact_form');
}
}
}
class Base_Controller
{
protected function render_view($view_name,$data = array())
{
/*
* I also think render_view should take care of loading the layout, and then inject the content into the middle of the layout file,
* so that you aren't trapping yourself to a specific layout, and repeating the header and footer inside of every view file
*/
extract($data); //places all $data variables into the local scope.. very clean and ezy ;].
require($this->root_directory.DS."$view_name.php");
}
/**********************************/
public function _no_action($view_name) //Called if there is no corresponding action
{
/* You can use method_exists to test if a method exists within the controller, if it does not exist,
* you can then call this function, and pass it the name of the view that is attempting to be opened
*/
if($this->view_exists($view_name))
{
$this->render_view($view_name,$data);
}
else
{
$this->render404();
}
}
}