在路由框架代码中不断给出未定义的变量错误

Keeps giving Undefined variable error in routing framework code

我需要做一个简单的路由系统。

我需要在键入 url 时重定向 localhost/user/login 它需要转到 UserController 文件,并且与键入 localhost 时完全相同转到 HomeController。

它一直给出一个未定义变量的错误,当我 / 到一个页面时它一直给出一个未定义的变量错误。

$_server 更改为 $_SERVER 时根本不起作用。这是它在使用 $_server:

时给出的错误

Notice: Undefined variable: _server in C:\xampp\htdocs\src\index.php on line 2

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\src\index.php on line 2

Notice: Undefined offset: 1 in C:\xampp\htdocs\src\index.php on line 3

下面是我使用的文件

<?php
$linkExplode = explode("/", $_SERVER['REQUEST_URI']);

if (empty($linkExplode[1] && empty($linkExplode[2]))) {
    $linkExplode[1] = 'Home';
    $linkExplode[2] = 'index';
}

$controller = empty($linkExplode[1]) ? 'home' : $linkExplode[1];
$method = empty($linkExplode[2]) ? 'index' : $linkExplode[2];

if (file_exists('./Controllers/' . ucfirst($controller) . 'Homecontroller.class.php')) {
    require('./Controllers/' . ucfirst($controller) . 'UserController.class.php');
    $classname = ucfirst($controller) . 'Controller';
    $class = new $classname();

    if (method_exists($class, $method)) {
        $class->$method();
    } else {
        http_response_code(404);
        die;
    }
} else {
    http_response_code(404);
    die;
}

家庭控制器

<?php
class HomeController
{
    public function index() 
    {
        echo 'De home pagina!';
    }
}

用户控制器

<?php
class UserController
{
    public function login() 
    {
        echo 'De login pagina!';
    }
}

Htacces

RewriteEngine On

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

RewriteRule ^(.+)$ src/index.php?url= [QSA,L]
DirectoryIndex src/index.php

目前Var_Dumping $_SERVER 回馈

array(58) { ["REDIRECT_MIBDIRS"]=> string(24) "C:/xampp/php/extras/mibs" ["REDIRECT_MYSQL_HOME"]=> string(16) "\xampp\mysql\bin" ["REDIRECT_OPENSSL_CONF"]=> string(31) "C:/xampp/apache/bin/openssl.cnf" ["REDIRECT_PHP_PEAR_SYSCONF_DIR"]=> string(10) "\xampp\php" ["REDIRECT_PHPRC"]=> string(10) "\xampp\php" ["REDIRECT_TMP"]=> string(10) "\xampp\tmp" ["REDIRECT_STATUS"]=> string(3) "200" ["MIBDIRS"]=> string(24) "C:/xampp/php/extras/mibs" ["MYSQL_HOME"]=> string(16) "\xampp\mysql\bin" ["OPENSSL_CONF"]=> string(31) "C:/xampp/apache/bin/openssl.cnf" ["PHP_PEAR_SYSCONF_DIR"]=> string(10) "\xampp\php" ["PHPRC"]=> string(10) "\xampp\php" ["TMP"]=> string(10) "\xampp\tmp" ["HTTP_HOST"]=> string(9) "localhost" ["HTTP_CONNECTION"]=> string(10) "keep-alive" ["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0" ["HTTP_SEC_CH_UA"]=> string(64) ""Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"" ["HTTP_SEC_CH_UA_MOBILE"]=> string(2) "?0" ["HTTP_SEC_CH_UA_PLATFORM"]=> string(9) ""Windows"" ["HTTP_UPGRADE_INSECURE_REQUESTS"]=> string(1) "1" ["HTTP_USER_AGENT"]=> string(114) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" ["HTTP_ACCEPT"]=> string(135) "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" ["HTTP_SEC_FETCH_SITE"]=> string(10) "cross-site" ["HTTP_SEC_FETCH_MODE"]=> string(8) "navigate" ["HTTP_SEC_FETCH_USER"]=> string(2) "?1" ["HTTP_SEC_FETCH_DEST"]=> string(8) "document" ["HTTP_ACCEPT_ENCODING"]=> string(17) "gzip, deflate, br" ["HTTP_ACCEPT_LANGUAGE"]=> string(35) "nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7" ["HTTP_COOKIE"]=> string(36) "PHPSESSID=auct5lh5cga85hmln0o54kfjf4" ["PATH"]=> string(964) "C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Users\jeann\AppData\Local\Android\Sdk\emulator;C:\ProgramData\ComposerSetup\bin;C:\Users\jeann\AppData\Local\Microsoft\WindowsApps;C:\Users\jeann\AppData\Local\Programs\Microsoft VS Code\bin;C:\PHP;C:\MinGW\bin;C:\Users\jeann\AppData\Roaming\npm;C:\Users\jeann\Pictures\gradle-7.1.1\bin;C:\Users\jeann\AppData\Local\Android\Sdk\emulator;C:\Users\jeann\AppData\Local\Android\Sdk\tools;C:\Users\jeann\AppData\Local\Android\Sdk\platform-tools;C:\Users\jeann\AppData\Roaming\Composer\vendor\bin" ["SystemRoot"]=> string(10) "C:\WINDOWS" ["COMSPEC"]=> string(27) "C:\WINDOWS\system32\cmd.exe" ["PATHEXT"]=> string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC" ["WINDIR"]=> string(10) "C:\WINDOWS" ["SERVER_SIGNATURE"]=> string(95) "
Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11 Server at localhost Port 80
" ["SERVER_SOFTWARE"]=> string(47) "Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11" ["SERVER_NAME"]=> string(9) "localhost" ["SERVER_ADDR"]=> string(3) "::1" ["SERVER_PORT"]=> string(2) "80" ["REMOTE_ADDR"]=> string(3) "::1" ["DOCUMENT_ROOT"]=> string(15) "C:/xampp/htdocs" ["REQUEST_SCHEME"]=> string(4) "http" ["CONTEXT_PREFIX"]=> string(0) "" ["CONTEXT_DOCUMENT_ROOT"]=> string(15) "C:/xampp/htdocs" ["SERVER_ADMIN"]=> string(20) "postmaster@localhost" ["SCRIPT_FILENAME"]=> string(29) "C:/xampp/htdocs/src/index.php" ["REMOTE_PORT"]=> string(5) "51472" ["REDIRECT_URL"]=> string(11) "/user/login" ["REDIRECT_QUERY_STRING"]=> string(14) "url=user/login" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" ["REQUEST_METHOD"]=> string(3) "GET" ["QUERY_STRING"]=> string(14) "url=user/login" ["REQUEST_URI"]=> string(11) "/user/login" ["SCRIPT_NAME"]=> string(14) "/src/index.php" ["PHP_SELF"]=> string(14) "/src/index.php" ["REQUEST_TIME_FLOAT"]=> float(1636987064.7515) ["REQUEST_TIME"]=> int(1636987064) }

首先,请确保您的 HTACCESS 路由有效。如果确实如此,并会将您带到正确的页面,然后继续下一步。

第二步,了解您的 PHP 代码,您需要一些矛盾和修复。

<?php
$linkExplode = explode("/", $_SERVER['REQUEST_URI']);

//YOU CAN REMOVE THIS WHOLE IF STATEMENT
if (empty($linkExplode[1] && empty($linkExplode[2]))) {
    $linkExplode[1] = 'Home';
    $linkExplode[2] = 'index';
}

//You should check that the $linkExplode isset as well as not empty
$controller = isset($linkExplode[1]) && !empty($linkExplode[1]) ? $linkExplode[1] : "Home";
$method = isset($linkExplode[2]) && !empty($linkExplode[2]) ? $linkExplode[2] : "index";

// Here we remove the 'hardcoded HOME and USER from the string 'Controller.class.php' 
// AND you also need to make sure that you require the same file your verifying is actually there.
if (file_exists('./Controllers/' . ucfirst($controller) . 'Controller.class.php')) {
    require('./Controllers/' . ucfirst($controller) . 'Controller.class.php');
    $classname = ucfirst($controller) . 'Controller';
    $class = new $classname();

    if (method_exists($class, $method)) {
        $class->$method();
    } else {
        http_response_code(404);
        die;
    }
} else {
    http_response_code(404);
    die;
}