Sas*_*sha 3 php routing routes altorouter
出于某种原因,我无法启动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();
Run Code Online (Sandbox Code Playgroud)
autoload.php:
<?php
require_once('app/Router.php');
Run Code Online (Sandbox Code Playgroud)
til*_*llz 10
你的问题是AltoRouter,根据文档(与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');
}
Run Code Online (Sandbox Code Playgroud)
瞧 - 现在你会得到你想要的输出!