首先,我想告诉你我的目录结构
- /var/www
|- /social_network/
|- index.php
|- /application/
|-/controllers/
|-user.php
|- /models/
|- framework.php
|-/tmp/
在我的 index.php 中,我包含 application/framework.php 这是我的 framework.php 代码
<?php
spl_autoload_register();
class Framework{
public function load($page, $data){
if(is_array($data)){
extract($data);
}
include "views/".$page;
}
}
$object = "controller\\$controller";
$object = new $object;
if(method_exists($object,"$method")){
$object->$method();
} else {
show_404();
}
?>
现在使用上面的代码,自动加载我的类工作正常。如果您想知道 $method 和 $controller 来自 index.php,具体取决于 URI。
现在我的一个朋友告诉我这是错误的做法,所以我将代码更改为
<?php
function autoload_controller($controller){
include "$controller.php";
}
spl_autoload_register('autoload_controller');
class Framework{
public function load($page, $data){
if(is_array($data)){
extract($data);
}
include "views/".$page;
}
}
$object = "controller\\$controller";
$object = new $object;
if(method_exists($object,"$method")){
$object->$method();
} else {
show_404();
}
?>
现在我在下面收到此错误
Warning: include(controller\User.php): failed to open stream: No such file or directory in /var/www/social_network/application/framework.php on line 3 Warning: include(): Failed opening 'controller\User.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/social_network/application/framework.php on line 3 Fatal error: Class 'controller\User' not found in /var/www/social_network/application/framework.php on line 16
我该如何解决这个问题,我已经在这几个小时了,如果可能的话,请告诉我如何使用自动加载从控制器类内部的模型目录中调用模型谢谢