PHP AltoRouter - 无法获取 GET 请求
PHP AltoRouter - can't get GET request
出于某种原因,我无法启动 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 (and in contrast to the 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');
}
瞧瞧 - 现在您将获得所需的输出!
出于某种原因,我无法启动 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 (and in contrast to the 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');
}
瞧瞧 - 现在您将获得所需的输出!