Php 将主控制器映射为AltoRouter中的默认控制器
这是index.phpPhp 将主控制器映射为AltoRouter中的默认控制器,php,model-view-controller,composer-php,router,altorouter,Php,Model View Controller,Composer Php,Router,Altorouter,这是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
<?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';
}
}
并将其保存为app/controllerdirectroy中的seprate文件home\u controller.php,因为它不起作用
我知道路由器无法定位home_controller类,因此不会显示其内容(如果我直接包含home_controller.php文件,它也会正常工作)
我的问题是,如何将主控制器映射为默认值,它位于不同的目录中?看起来您没有使用它来安装软件包。这是PHP中的标准方式
1.安装编写器
- ,具体取决于您的操作系统
2.从命令行调用编写器 转到项目的根目录,打开命令行并键入:
composer require altorouter/altorouter
您可以在包的Github页面上的composer.json
中找到包名altorouter/altorouter
3.将加载的文件添加到
index.php
现在您已经安装了路由器包。下一步是将所有composer加载的文件添加到应用程序中。只需替换包含“library/altorouter.php”代码>包含以下内容:
<?php
# index.php
require_once __DIR__ . '/vendor/autoload.php';
阅读更多关于
要使用此选项更新/vendor/autoload.php
,只需从命令行调用:
composer dump-autoload
应该是这样。如果您遇到任何问题,请告诉我哪一点。您当前的类自动加载程序不包括app/controller
目录中的类。您需要为该特定目录设置自动加载。你知道怎么做,对吧?谢谢你。。。我没有跟随任何一个台阶爬上山顶。我接受你的回答,做完后再给你回复。非常感谢。
{
"autolaod": {
"classmap": ["app"]
}
}
composer dump-autoload