0

我一直在尝试使用 altorouter 获取 Home 控制器的 index() 方法,但无法。我搜索了几个地方,但找不到任何帮助。

这是 index.php

<?php

include 'system/startup.php';
include 'library/AltoRouter.php';

// Router
$router = new AltoRouter();
// Change here before upload
$router->setBasePath('/demo');
$router->map('GET|POST','/', 'home#index', 'home');

// match current request
$match = $router->match();

if( $match && is_callable( $match['target'] ) ) {
    call_user_func_array( $match['target'], $match['params'] ); 
} else {
    // no route matched
    header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}

目录>控制器目录中的主控制器。

<?php
class home {
    public function index() {
        echo 'home';
    }
}

任何人都可以使用或曾经使用过这个 altorouter 指南吗?

PS我在startup.php文件中有自动加载功能(包含在index.php的顶部)

4

1 回答 1

0

这是旧线程,但这可以帮助您。您需要以不同的方式匹配请求:

<?php

include 'system/startup.php';
include 'library/AltoRouter.php';

// Router
$router = new AltoRouter();
$router->setBasePath('/demo');
$router->map('GET|POST','/', 'home#index', 'home');
$match = $router->match();

if ($match === false) {
    // here you can handle 404
} else {
    list( $controller, $action ) = explode( '#', $match['target'] );
    if ( is_callable(array($controller, $action)) ) {
        call_user_func_array(array($controller,$action), array($match['params']));
    } else {
        // here your routes are wrong.
        // Throw an exception in debug, send a  500 error in production
    }
}
于 2017-10-07T18:30:02.200 回答