PHP AltoRouter - 无法获得 GET 请求 [英] PHP AltoRouter - can't get GET request

查看:55
本文介绍了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!

这篇关于PHP AltoRouter - 无法获得 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆