【发布时间】:2015-02-20 12:32:47
【问题描述】:
我正在构建一个小型 PHP MVC 框架,这是我的文件夹结构
/app
/controllers
/models
/views
/templates
/config
/config.php
/core
/Controller.php
/Router.php
/init.php
/index.php
在作为前端控制器的 index.php 内部,我有这段代码需要 /app/core/init.php 中的 init.php
index.php
<?php
require_once 'app/core/init.php'
$Router = new Router();
$Controller = new Controller();
?>
app/core/init.php
<?php
require_once 'Controller.php';
require_once 'Router.php';
?>
init.php 需要 /core 目录中的每个基本控制器和类,包括 Controller.php 和 Router.php,这里 index.php 还实例化类
此时一切正常,因为我通过在 Controller.php 和 Router.php 中创建构造函数来测试这一点,所以这两个文件中的代码将是这样的
app/core/Controller.php
<?php
class Controller {
public function __construct() {
echo 'OK!';
}
}
?>
app/core/Router.php
<?php
class Router {
public function __construct() {
echo 'OK!';
}
}
?>
在 index.php 内它回显 OK!因为这些类是正确实例化的,但问题是当我想从位于 /app/core/Controller.php 的 Controller.php 中包含位于 /app/config/config.php 中的 config.php 时,此代码
<?php
class Controller {
public function __construct() {
require_once '../config/config.php';
}
}
?>
每当我这样做时,它都会返回此错误
Controller::include(../config/config.php) [controller.include]: failed to open stream: No such file or directory in C:\AppServ\www\myapp\app\core\Controller.php on line 6
和
Controller::include() [function.include]: Failed opening '../config/config.php' for inclusion (include_path='.;C:\php5\pear') in C:\AppServ\www\myapp\app\core\Controller.php on line 6
我认为我使用了正确的位置,我在 /app/core/Controller.php 中工作并且想要要求 /app/config/config.php。我使用 ../
返回一个目录那为什么我不能要求该文件?
【问题讨论】:
-
了解自动加载。在构造函数中包含一些东西在很多层面上都是错误的。
-
你为什么要在控制器中加载
config?你的方法不适合有一个工作的 mvc
标签: php