php 在工艺路线框架代码中不断出现未定义变量错误

amrnrhlw  于 2022-12-02  发布在  PHP
关注(0)|答案(2)|浏览(112)

我需要做一个简单的路由系统。
我需要重定向时,我键入的url localhost/user/login它需要去UserController文件,也完全相同,只是当你键入localhost它需要去HomeController。

  • 检查请求的控制器是否存在,如果存在,则“包含”它。
  • 检查请求的方法是否存在,如果存在,则调用该方法。
  • 仅包括对该URL重要的控制器
  • 如果控制器不存在,则打印“404 Not found(404未找到)”消息。
  • 如果该方法不存在,则打印404 Not found消息。
  • 同时为404页返回404状态代码。

它不断给出未定义变量的错误,当I /到页面时,它不断给出未定义变量的错误。
$_server更改为$_SERVER时,它根本不起作用。这是使用$_server时出现的错误:
注意:未定义的变量:第2行的C:中的_服务器
注意:尝试访问第2行C:\xampp\htdocs\src\index.php中类型为null的值的数组偏移量
注意:未定义的偏移:第3行中的第1行

下面是我使用的文件

<?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!';
    }
}

访问权限

RewriteEngine On

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

RewriteRule ^(.+)$ src/index.php?url=$1 [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) }
mm5n2pyu

mm5n2pyu1#

第一件事,是确保你的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;
}
zi8p0yeb

zi8p0yeb2#

试着检查你使用的PHP版本。我在使用8.1时遇到了几乎类似的错误,必须降级到原生7.4,错误就消失了。这是我的错误PHP Warning: Undefined array key "SERVER_SIGNATURE"可以帮助。

相关问题