altorouter路线不起作用

时间:2016-02-10 09:42:28

标签: php routing routes altorouter

我试图使用altorouter来设置我的php项目的路由映射,此时文件routes.php就是这个

<?php
$router = new AltoRouter();
$router->setBasePath('/home/b2bmomo/www/');
/* Setup the URL routing. This is production ready. */
// Main routes that non-customers see
$router->map('GET','/', '', 'home');
$router->map( 'GET', '/upload.php', 'uploadexcel');

$match = $router->match();

// 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');
}
?>

我项目的主要目录中有2个文件,index.php和upload.php,有什么不对?

2 个答案:

答案 0 :(得分:0)

是否已根据the altorouter site?

修改.htaccess文件以进行重写

你的路线看错了。试试这样:

// 1. protocol - 2. route uri  -3. static filename -4. route name
$router->map('GET','/uploadexcel', 'upload.php', 'upload-route');

因为看起来你想要一个静态页面(不是控制器)试试这个(允许两者):

if($match) {
        $target = $match["target"];
        if(strpos($target, "#") !== false) { //-> class#method as set in routes above, eg 'myClass#myMethod' as third parameter in mapped route
            list($controller, $action) = explode("#", $target);
            $controller = new $controller;
            $controller->$action($match["params"]);
        } else { 
            if(is_callable($match["target"])) {
                call_user_func_array($match["target"], $match["params"]); //call a function
            }else{
                require $_SERVER['DOCUMENT_ROOT'].$match["target"]; //for static page
            }
        }
    } else {
        require "static/404.html";
        die();
    }

这几乎就是这里:https://m.reddit.com/r/PHP/comments/3rzxic/basic_routing_in_php_with_altorouter/?ref=readnext_6

并摆脱该基线路线。

祝你好运

答案 1 :(得分:0)

你可以通过&#34; call_user_func_array&#34;来运行类#功能:

    if ($match) {

        if (is_string($match['target']) && strpos($match['target'], '#') !== false) {
            $match['target'] = explode('#', $match['target']);
        }

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