我想在 MVC 架构中创建一个网站,而不使用任何框架。文件/文件夹结构(模块/视图/模型)是什么。
编辑:我知道 Zend,CakePHP。但我的问题是我们如何在Simple PHP中构建结构。我不想使用任何框架,因为我需要最大的性能/速度。我听说框架比简单的 PHP Web 应用程序要慢, 这就是我选择简单 PHP 的原因。
编辑 2: 我希望通过 index.php 调用每个网页,
我想在 MVC 架构中创建一个网站,而不使用任何框架。文件/文件夹结构(模块/视图/模型)是什么。
编辑:我知道 Zend,CakePHP。但我的问题是我们如何在Simple PHP中构建结构。我不想使用任何框架,因为我需要最大的性能/速度。我听说框架比简单的 PHP Web 应用程序要慢, 这就是我选择简单 PHP 的原因。
编辑 2: 我希望通过 index.php 调用每个网页,
你可以有这样的结构:
root folder/
index.php
views/
models/
controllers/
util/
index.php 是前端加载器 - 所有 URL 都经过此处理,它会根据 URL 确定要调用哪个控制器。您可以使用 mod_rewrite 从 URL 中删除 index.php。
views/ 文件夹包含您的 html、rss、xml 等视图文件 - 您将从控制器中填充这些值。您可以在这里使用 Smarty,但坦率地说,我不明白这一点。
models/ 文件夹将包含您的 ORM 或 DB 连接代码。模型很容易从头开始编写。
controllers/ 文件夹将包含每个 URL 的一个控制器文件。比如说,books.php 用于处理书籍的 CRUD URL。每个控制器都应该加载模型并使用数据来填充视图。
util/ 文件夹可以包含任何不是模型或控制器的类。安全、会话管理等辅助内容。
看到您自己编写它,您可以消除杂乱无章并保持简单。但你可能会发现这实际上是一项比你想象的更大的工作。我知道你不想使用一个,但 Code Igniter 确实让事情变得非常简单,很轻,并且不会像 Cake 和 Symfony 等其他框架那样为你做太多事情。
PHP 的原始创建者发布了一篇关于这类事情的好文章。
http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html
在使用 Zend Framework 之前,我曾经做过类似的事情。
这是文件/文件夹结构:
/
/views
/layouts
/controllers
/library
/etc
/data
Site.php
index.php
视图:包含所有模板,每个控制器/动作一个
布局:一个布局,它获取一个包含要包含的文件名的 var(来自视图)
控制器:控制器
库:项目所需的所有额外工具
等:文件等。
数据:用于文件上传
用于初始化整个项目的 Site.php 文件,一种由 index.php 调用的引导程序
index.php :调用引导程序
<?php
class Site
{
protected $_action = NULL;
protected $_contentFile = NULL;
protected $_args = array();
protected $_headTitle = NULL;
protected $_headerStack = array();
public function __construct ($action)
{
$this->setAction($action);
$this->setArgs();
}
public function setHeader($name = null, $value = null)
{
if (null != $name && null != $value)
{
$this->_headerStack[$name] = $value;
}
}
public function sendHeaders()
{
if (null != $this->_headerStack)
{
foreach ($this->_headerStack as $key => $value)
{
header($key . ':' . ' ' . $value);
}
}
return $this;
}
public function setAction($action)
{
$this->_action = (! empty($action) ) ? $action : 'error';
return $this;
}
public function setArgs()
{
$this->_args['GET'] = $_GET;
$this->_args['POST'] = $_POST;
}
public function getParam($name)
{
if ( ! empty($this->_args['GET'][$name]))
{
return $this->_args['GET'][$name];
} else {
return null;
}
}
public function getParams()
{
return $this->_args['GET'];
}
public function getPost()
{
return $this->_args['POST'];
}
public function preRun()
{
if (is_file('views/' . $this->_action . '.phtml'))
{
$content = 'views/' . $this->_action . '.phtml';
$this->setContentFile($content);
}
if (is_file('library/' . ucfirst($this->_action) . 'Action.php'))
{
require_once 'library/' . ucfirst($this->_action) . 'Action.php';
if (function_exists ('init'))
{
init();
}
}
}
public function run()
{
$this->sendHeaders();
$this->preRun();
require_once 'layouts/main.phtml';
$this->sendHeaders();
}
public function setContentFile($content)
{
$this->_contentFile = ( !empty($content) ) ? $content : '';
return $this;
}
public function getContent()
{
require_once $this->_contentFile;
}
public function setHeadTitle($title)
{
$this->_headTitle = $title;
return $this;
}
public function getHeadTitle()
{
return $this->_headTitle;
}
}
然后,在我的索引中我做了:
$action = $_GET['action'];
$site = new Site($action);
$site->run();
为方便起见,我删除了一些额外的安全检查...
然后,您可以将其扩展为包括从控制器调用的模型目录等...
我真的很喜欢symfony树结构 http://andreiabohner.files.wordpress.com/2007/03/cheatsheetsymfony001_enus.pdf
关于您的编辑,我建议 symfony 目录结构(或它的子集,根据您的需要),不要使用 symfony。
关于框架的性能,这取决于。好的框架知道他们的问题,并使用缓存和其他优化技术;使用轻量级 http 服务器而不是 apache 可能是另一个问题。但一切都取决于您的特定需求。
正如用户meder 所建议的,尝试非常简单的 mvc 框架web2bb。还有我的移植版,针对 PHP 5.2 进行了增强和优化。您可以在此处通过 svn 访问其源代码:
源代码已嵌入 BLOG 示例应用程序。