【问题标题】:altorouter routes doesn't workaltorouter 路由不起作用
【发布时间】:2016-02-10 09:42:28
【问题描述】:

我正在尝试使用 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');
}
?>

我的项目主目录有两个文件,index.php和upload.php,怎么了?

【问题讨论】:

    标签: php routing routes altorouter


    【解决方案1】:

    您是否修改了您的 .htaccess 文件以按照 the altorouter site? 重写

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

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

    并摆脱该基本路径行。

    祝你好运

    【讨论】:

      【解决方案2】:

      你通过“call_user_func_array”运行class#function:

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

      【讨论】:

        猜你喜欢
        • 2016-11-04
        • 1970-01-01
        • 2017-10-31
        • 2019-11-03
        • 2018-02-22
        • 1970-01-01
        • 1970-01-01
        • 2011-11-07
        相关资源
        最近更新 更多