提问者:小点点

PHP路由未在所有路径上正常工作


我想使用php来路由我的网站路径。在各种论坛中,我遇到了以下代码。主页/home一切正常!对于所有其他页面/路径,如/bewerben,将显示一个空白的白色页面。控制台传输以下错误:

  • GEThttps://test.sestertius.info/static/js/main.711e96fc.chunk.jsnet::ERR_中止404
  • GEThttps://test.sestertius.info/static/css/main.b2c5178a.chunk.cssnet::ERR_中止404
  • GEThttps://test.sestertius.info/static/js/2.b2024826.chunk.jsnet::ERR_中止404
  • GEThttps://test.sestertius.info/static/js/main.711e96fc.chunk.jsnet::ERR_中止404

前端是用React编写的。最后,我想要一个这样的URL,用php编写:

我的文件夹结构:

/root

  • /sestertius.info
  • /sestertius.jobs
  • . htaccess
  • Route.php
  • index.php

我的索引。php文件:

<?php

include('Route.php');

Route::add('/home', function() {
        $main = file_get_contents("sestertius.info/index.html");
        echo $main;
});

Route::add('/bewerben', function() {
        $main = file_get_contents("sestertius.jobs/index.html");
        echo $main;
});


Route::run('/');
?>

我的. htaccess文件

DirectoryIndex index.php

# enable apache rewrite engine
RewriteEngine on

# set your rewrite base
# Edit this in your init method too if you script lives in a subfolder
RewriteBase /

# Deliver the folder or file directly if it exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Push every request to index.php
RewriteRule ^(.*)$ index.php [QSA]


共1个答案

匿名用户

我的路线。php文件

class Route{

  private static $routes = Array();
  private static $pathNotFound = null;
  private static $methodNotAllowed = null;

  public static function add($expression, $function, $method = 'get'){
    array_push(self::$routes,Array(
      'expression' => $expression,
      'function' => $function,
      'method' => $method
    ));
  }

  public static function pathNotFound($function){
    self::$pathNotFound = $function;
  }

  public static function methodNotAllowed($function){
    self::$methodNotAllowed = $function;
  }

  public static function run($basepath = '/'){

    // Parse current url
    $parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri

    if(isset($parsed_url['path'])){
      $path = $parsed_url['path'];
    }else{
      $path = '/';
    }

    // Get current request method
    $method = $_SERVER['REQUEST_METHOD'];

    $path_match_found = false;

    $route_match_found = false;

    foreach(self::$routes as $route){

      // If the method matches check the path

      // Add basepath to matching string
      if($basepath!=''&&$basepath!='/'){
        $route['expression'] = '('.$basepath.')'.$route['expression'];
      }

      // Add 'find string start' automatically
      $route['expression'] = '^'.$route['expression'];

      // Add 'find string end' automatically
      $route['expression'] = $route['expression'].'$';

      // echo $route['expression'].'<br/>';

      // Check path match   
      if(preg_match('#'.$route['expression'].'#',$path,$matches)){

        $path_match_found = true;

        // Check method match
        if(strtolower($method) == strtolower($route['method'])){

          array_shift($matches);// Always remove first element. This contains the whole string

          if($basepath!=''&&$basepath!='/'){
            array_shift($matches);// Remove basepath
          }

          call_user_func_array($route['function'], $matches);

          $route_match_found = true;

          // Do not check other routes
          break;
        }
      }
    }

    // No matching route was found
    if(!$route_match_found){

      // But a matching path exists
      if($path_match_found){
        header("HTTP/1.0 405 Method Not Allowed");
        if(self::$methodNotAllowed){
          call_user_func_array(self::$methodNotAllowed, Array($path,$method));
        }
      }else{
        header("HTTP/1.0 404 Not Found");
        if(self::$pathNotFound){
          call_user_func_array(self::$pathNotFound, Array($path));
        }
      }

    }

  }

}