由于某种原因,我无法启动AltoRouter。我正在尝试最基本的呼叫,但没有任何反应。我怎样才能让它工作?我的索引.php文件如下所示:
<?php
include('settings/autoload.php');
use appAltoRouter;
$router = new AltoRouter;
$router->map('GET', '/', function(){
echo 'It is working';
});
$match = $router->match();
自动加载.php:
<?php
require_once('app/Router.php');
您的问题是,根据文档(与似乎具有相同语法的Slim框架相反),AltoRouter不会为您处理请求,它只匹配它们。因此,通过调用$router->match()
您可以获得以您喜欢的任何方式处理请求所需的所有信息。如果你只想调用闭包函数,只需修改你的代码:
<?php
// include AltoRouter in one of the many ways (Autoloader, composer, directly, whatever)
$router = new AltoRouter();
$router->map('GET', '/', function(){
echo 'It is working';
});
$match = $router->match();
// Here comes the new part, taken straight from the docs:
// call closure or throw 404 status
if( $match && is_callable( $match['target'] ) ) {
call_user_func_array( $match['target'], $match['params'] );
} else {
// no route was matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
瞧 - 现在您将获得所需的输出!