【发布时间】:2018-03-29 02:06:35
【问题描述】:
这是index.php
<?php
include 'library/altorouter.php';
$router = new AltoRouter();
$router->setBasePath('/AltoRouter');
$router->map('GET','/', 'home_controller#index', 'home');
$router->map('GET','/content/[:parent]/?[:child]?', 'content_controller#display_item', 'content');
$match = $router->match();
// not sure if code after this comment is the best way to handle matched routes
list( $controller, $action ) = explode( '#', $match['target'] );
if ( is_callable(array($controller, $action)) ) {
$obj = new $controller();
var_dump($obj);
call_user_func_array(array($obj,$action), array($match['params']));
} else if ($match['target']==''){
echo 'Error: no route was matched';
} else {
echo 'Error: can not call '.$controller.'#'.$action;
}
// content_controller class file is autoloaded
class home_controller {
public function index() {
echo 'hi from home';
}
}
而且效果很好。 home_controller 类应该是默认控制器。
问题是,当我删除类 home_controller 时
class home_controller {
public function index() {
echo 'hi from home';
}
}
并将其保存为单独的文件 home_controller.php 在 app/controller 目录中它不起作用。
我知道路由器无法找到 home_controller 类,因此不会显示它的内容(如果我直接包含文件 home_controller.php,它会再次正常工作)。
我的问题是,如何将 home_controller 映射为默认值,它位于不同的目录中?
【问题讨论】:
-
您当前的类自动加载器不包括
app/controller目录中的类。您需要为该特定目录设置自动加载。你知道怎么做,对吧?
标签: php model-view-controller composer-php router altorouter