I need to make a simple routing system.
I need to redirect when i type the url localhost/user/login
it need to go to the UserController file, and also exactly the same with just when you type localhost
it needs to go to the HomeController.
- Check whether the requested controller exists and if so, 'include' it.
- Check if the requested method exists, and if so, call it.
- Include only the controller that is important for that URL
- If the controller does not exist, print a 404 Not found message.
- If the method does not exist, print a 404 Not found message.
- Also return a 404 status code for 404 pages.
It keeps giving an error with undefining a variable, and when I /
to a page it keeps giving an undefined variable error.
When change the $_server
to $_SERVER
it doesn't work at all. This is the error it gives when using $_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
Down here are the files I use.
<?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;
}
HomeController
<?php
class HomeController
{
public function index()
{
echo 'De home pagina!';
}
}
UserController
<?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=$1 [QSA,L]
DirectoryIndex src/index.php
Currently Var_Dumping $_SERVER Giving this back
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) }
CodePudding user response:
First thing, is make sure you HTACCESS route works. If it does and brings you to the correct page then on to the next step.
Second step understanding your PHP code here, there are a couple contradictions and fixes you need.
<?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;
}