出于某种原因,我无法启动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');
答案 0 :(得分:10)
你的问题是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');
}
瞧瞧 - 现在你会得到你想要的输出!