PHP AltoRouter - 无法获得 GET 请求
由于某种原因,我无法启动 AltoRouter.我正在尝试最基本的调用,但什么也没发生.我怎样才能让它工作?我的 index.php 文件如下所示:
For some reason I am not able to start AltoRouter. I am trying the most basic call, but nothing is happening. How can I make it work? My index.php file looks like this:
<?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(与 Slim Framework 形成对比,似乎具有相同的语法),获胜不会为您处理请求,它只会匹配他们.因此,通过调用 $router->match()
,您可以获得以您喜欢的任何方式处理请求所需的所有信息.如果您只想调用闭包函数,只需修改您的代码:
Your Problem is that AltoRouter, according to the documentation (and in contrast to the Slim Framework, which seems to have the the same syntax), won't process the request for you, it only matches them.
So by calling $router->match()
you get all the required information to process the request in any way you like.
If you just want to call the closure-function, simply modify your code:
<?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');
}
瞧——现在你会得到你想要的输出!
And voilà - now you'll get your desired output!