背景
在过去的几个月里,我一直在学习各种教程,目前正在尝试理解PHP框架。
我这样做的方法之一是从零开始设计我自己的非常简单的MVC框架。
我正在尝试重新考虑应用程序(我已经使用意大利面过程PHP构建了这个应用程序)。这个应用程序有一个教师前端和一个管理员后端.
我想把关注点分开,让URL像这样
http://example.com/{module}/{controller}/{method}/{param-1}/{param-2}到目前为止,我拼凑在一起的MVC框架并不处理“模块”的路由(如果这不是正确的术语,我很抱歉),只有控制器/方法/params。
因此,我将public_html从应用程序逻辑中分离出来,在/ app /文件夹中指定了两个文件夹,默认的“学习模块”和“管理模块”,因此目录树如下所示:

显然,这种设计模式是"H"MVC?
我的解决方案
我基本上是在使用is_dir();函数来检查是否有一个“模块”目录(如"admin"),然后取消第一个$url[0]数组元素的设置,并将数组重新索引为0.然后根据URL更改控制器路径.密码应该更清楚..。
<?php
class App
{
protected $_module = 'learn'; // default module --> learn
protected $_controller = 'home'; // default controller --> home
protected $_method = 'index'; // default method --> index
protected $_params = []; // default paramatars --> empty array
public function __construct() {
$url = $this->parseUrl(); // returns the url array
// Checks if $url[0] is a module else it is a controller
if (!empty($url) && is_dir('../app/' . $url[0])) {
$this->_module = $url[0]; // if it is a model then assign it
unset($url[0]);
if (!empty($url[1]) && file_exists('../app/' . $this->_module . '/controllers/' . $url[1] . '.php')) {
$this->_controller = $url[1]; // if $url[1] is also set, it must be a controller
unset($url[1]);
$url = array_values($url); // reset the array to zero, we are left with {method}{param}{etc..}
}
// if $url[0] is not a module then it might be a controller...
} else if (!empty($url[0]) && file_exists('../app/' . $this->_module . '/controllers/' . $url[0] . '.php')) {
$this->controller = $url[0]; // if it is a controller then assign it
unset($url[0]);
$url = array_values($url); // reset the array to zero
} // else if url is empty default {module}{controller}{method} is loaded
// default is ../app/learn/home/index.php
require_once '../app/' . $this->_module . '/controllers/' . $this->_controller . '.php';
$this->_controller = new $this->_controller;
// if there are methods left in the array
if (isset($url[0])) {
// and the methods are legit
if (method_exists($this->_controller, $url[0])) {
// sets the method that we will be using
$this->_method = $url[0];
unset($url[0]);
} // else nothing is set
}
// if there is anything else left in $url then it is a parameter
$this->_params = $url ? array_values($url) : [];
// calling everything
call_user_func_array([$this->_controller, $this->_method], $this->_params);
}
public function parseUrl() {
// checks if there is a url to work with
if (isset($_GET['url'])) {
// explodes the url by the '/' and returns an array of url 'elements'
return $url = EXPLODE('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
}到目前为止,这似乎是为我工作,但是.
问题
我不确定这是否是这个问题的首选解决方案。正在为每一个页面请求调用is_dir()检查以减慢我的应用程序?
你会如何设计一个解决方案,或者我完全误解了这个问题?
非常感谢您的时间和考虑!!
发布于 2016-06-22 18:04:41
根据我的经验,我经常使用.htaccess文件将任何请求重定向到唯一的index.php文件,这两个文件都放在public_html文件夹中。.htaccess文件的内容如下(您的apache应该启用mod_rewrite):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>然后,在index.php文件中,您可以解析请求url、定义信任、公共变量、路径等.并包括必要的其他php文件。举个例子:
require( dirname( __FILE__ ) . '/your_routing.php' );
require( dirname( __FILE__ ) . '/your_configs.php' );
require( dirname( __FILE__ ) . '/admin/yourfile.php' );
...https://stackoverflow.com/questions/37974779
复制相似问题