0

我正在尝试使用 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,怎么了?

4

2 回答 2

0

you car run class#function via "call_user_func_array":

    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');
        }
    } 
于 2017-02-28T11:51:13.363 回答
0

您是否修改了 .htaccess 文件以根据altorouter 站点进行重写?

你的路线看起来不对。试试这样:

// 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

并摆脱那条基本路径线。

祝你好运

于 2016-02-24T10:20:53.240 回答