出于某种原因,我无法启动AltoRouter.我正在尝试最基本的电话,但一切都没有发生.我怎样才能使它工作?
我的index.php文件如下所示:
<?php
include('settings/autoload.php');
use app\AltoRouter;
$router = new AltoRouter;
$router->map('GET', '/', function(){
echo 'It is working';
});
$match = $router->match();
autoload.php:
<?php
require_once('app/Router.php');
解决方法:
你的问题是AltoRouter,根据documentation(与Slim Framework相比,似乎具有相同的语法),不会处理你的请求,它只匹配它们.
因此,通过调用$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');
}
瞧 – 现在你会得到你想要的输出!