【发布时间】:2016-04-28 23:48:14
【问题描述】:
这是我的 php 项目的结构
文件 index.php 和 config.php 位于根文件夹中。
在 config.php 文件中我定义了 libs 文件夹的路径
<?php
DEFINE('LIBS', 'app/Libs/');
这是 index.php 文件夹
<?php
require 'config.php';
function __autoload($class) {
require LIBS. $class. ".php";
}
$bootstrap = new Bootstrap();
$bootstrap->init();
//Composer autoloader
require 'vendor/autoload.php';
这是Bootstrap.php文件
<?php
class Bootstrap {
private $_url = null;
private $_controller = null;
private $_controllerPath = '/../controllers/';
private $_modelPath = 'models/';
private $_errorFile = 'error.php';
private $_defaultFile = 'home.php';
public function init(){
$this->_parseUrl();
if(empty($this->_url[0])){
$this->_loadDefaultController();
return false;
}
$this->_loadExistingController();
$this->_callControllerMethod();
}
private function _parseUrl(){
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$this->_url = explode('/', $url);
}
private function _loadDefaultController() {
require $this->_controllerPath . $this->_defaultFile;
$this->_controller = new Home();
$this->_controller->index();
}
private function _loadExistingController() {
$file = $this->_controllerPath . $this->_url[0] . '.php';
if(file_exists($file)) {
require $file;
$this->_controller = new $this->_url[0];
$this->_controller->loadModel($this->_url[0], $this->_modelPath);
} else {
$this->_error();
return false;
}
}
private function _callControllerMethod() {
if(isset($this->_url[2])) {
if(method_exists($this->_controller, $this->_url[1])) {
$this->_controller->{$this->_url[1]}($this->_url[2]);
} else {
$this->_error();
}
} else {
if(isset($this->_url[1])) {
if(method_exists($this->_controller, $this->_url[1])) {
$this->_controller->{$this->_url[1]}();
} else {
$this->_error();
}
} else {
$this->_controller->index();
}
}
}
private function _error() {
require $this->_controllerPath . $this->_errorFile;
$this->_controller = new Error();
$this->_controller->index();
exit;
}
}
问题是,Bootstrap 类以正确的方式检测 Home 控制器,但它没有找到其他控制器,例如用户控制器,并尝试使用错误控制器,但没有找到错误索引视图。
Warning: require(/../views/error/index.php): failed to open stream: No such file or directory in C:\xampp\htdocs\project\app\libs\View.php on line 9
Fatal error: require(): Failed opening required '/../views/error/index.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\project\app\libs\View.php on line 9
我猜路径有问题,但我不知道如何修复它。如何让 Bootstrap 找到其他控制器?也许我应该改变项目结构?这是我第一次尝试创造一些东西,所以我只是想弄清楚。
【问题讨论】:
-
你的意思是`$_controllerPath = '../controllers/';`?
-
不,在这种情况下,bootstrap 没有找到任何控制器,包括 Home 控制器。警告:需要(../controllers/home.php):无法打开流:第 48 行的 C:\xampp\htdocs\project\app\libs\Bootstrap.php 中没有这样的文件或目录致命错误:需要() : 在第 48 行的 C:\xampp\htdocs\project\app\libs\Bootstrap.php 中打开所需的 '../controllers/home.php' (include_path='.;C:\xampp\php\PEAR') 失败
-
在 Unix 上,文件名不区分大小写,但在 Windows 中是。将
home.php更改为Home.php -
错误是一样的 警告:require(../controllers/Home.php): failed to open stream: No such file or directory in C:\xampp\htdocs\project\app\libs\第 48 行的 Bootstrap.php 致命错误:require():在 C:\xampp\htdocs\ 中打开所需的 '../controllers/Home.php' (include_path='.;C:\xampp\php\PEAR') 失败project\app\libs\Bootstrap.php 在第 48 行
-
@Federico 在 *nix 中正好相反,文件名区分大小写,而 windows 则不区分。
标签: php