简单的PHP路由不起作用

时间:2016-10-05 21:49:44

标签: php routing

抱歉,我是初学者,所以问题可能只是一件小事。 目前我尝试实现一个简单的路由。我发现了一个教程(抱歉,它是德语:https://steampixel.de/einfaches-und-elegantes-url-routing-mit-php/

问题是: 如果我打电话给www.myproject.at/我希望我能看到我的模板这个路径,但我只看到404页面。 我找不到自己的错误,但我认为这是一个错误的错误。

我使用apache 2.4,我的vhost非常简单。 我在/ var / www中有所有myprojects 实际的根目录(我的index.php所在的位置)是/ var / www / PROJECT / www / 所以我配置了vhost。

我的htaccess

DirectoryIndex index.php
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php [QSA, L]

的config.php

private static $registry = Array();

public static function set($key, $value) {
    self::$registry[$key] = $value;
}

public static function get($key) {
    if (array_key_exists($key, self::$registry)) {
        return self::$registry[$key];
    }
    return FALSE;
}

Route.php

public static $routes = Array();
public static $routes404 = Array();
public static $path;

public static function init() {
    // cut url
    $parsedUrl = parse_url($_SERVER['REQUEST_URI']);
    if (isset($parsedUrl['path'])) {
        self::$path = trim($parsedUrl['path'], '/');
    } else {
        self::$path = '';
    }
}

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

public static function add404($function) {
    array_push(self::$routes404,$function);
}

public static function run(){
    $routeFound = FALSE;

    foreach(self::$routes as $route){
        if(Config::get('basepath')) {
            $route['expression'] = '('.Config::get('basepath').')/'.$route['expression'];
        }
        //Add 'find string start' automatically
        $route['expression'] = '^'.$route['expression'];
        //Add 'find string end' automatically
        $route['expression'] = $route['expression'].'$';
        //check match
        if(preg_match('#'.$route['expression'].'#',self::$path,$matches)) {
            //echo $expression;
            array_shift($matches);//Always remove first element. This contains the whole string
            if(Config::get('basepath')) {
                array_shift($matches);//Remove Basepath
            }

            call_user_func_array($route['function'], $matches);
            $route_found = TRUE;
        }
    }

    if($routeFound == FALSE) {
        foreach(self::$routes404 as $route404) {
            call_user_func_array($route404, Array(self::$path));
        }
    }
}

的index.php

require_once './core/vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem('./core/templates');
$twig = new Twig_Environment($loader, array(
    'cache' => './cache',
    'auto_reload' => 'true', # If it’s set to false, the templates will be reloaded only if you delete the contents of the cache directory
    ));

// includes for routing
include('core/php/Config.php');
include('core/php/Route.php');

// Config
Config::set('basepath','/');
// init routing
Route::init();

// base Route
Route::add('/', function() use ($twig) {
    $template = $twig->loadTemplate('surrounding.html');
    $template->display([]);
});

Route::add404(function($url) use ($twig) {
    //Send 404 Header
    header("HTTP/1.0 404 Not Found");
    $template = $twig->loadTemplate('404.html');
    $template->display([]);
});



Route::run();

感谢大家! 对于长篇文章感到抱歉,如果这只是一个愚蠢的问题。

1 个答案:

答案 0 :(得分:0)

首先,您不能将array_key_exists与array(1, 2, 3)这样的数组一起使用,它们只适用于像array(1 => 2, 3 => 4)这样的数组。原因是如果你在像array('hello', 'nice', 'crazy', 'lord')这样的数组上使用它,它将返回0,1,2,3而不是实际值。与其他类型的数组一起,它将返回值,我建议使用in_array使代码工作。

例如:

<?php
  if (in_array($key, self::$registry)) {
    return self::$registry[$key];
  }
  return FALSE;
?>
相关问题