emanjbollie79's avatar

Pages not rendering after moving controllers to separate directory

I have been doing so well until I moved all my controllers to a separate directory to start dealing with routing. None of my pages now renders, only the 'else{} condition. LOL. My else condition says "Gibberish"! LOL. I am laughing it off to manage the stress. Hehe!

$uri = $_SERVER['REQUEST_URI'];

if ($uri === '/') { require 'controllers/index.php'; } else if ($uri === '/about.php') { require 'controllers/about.php'; } else if ($uri === '/contact.php') { require 'controllers/contact.php'; } else if ($uri === '/blog.php') { require 'controllers/blog.php'; }else{ echo 'Gibberish'; }

0 likes
4 replies
LaryAI's avatar
Level 58

It sounds like you've restructured your project and moved your controllers into a separate directory. If your pages are not rendering after this change, it's likely that the paths to your controller files in the routing logic are incorrect. Here's how you can fix it:

  1. Make sure that the directory you moved your controllers into is correctly referenced in your routing logic.
  2. Ensure that the .php extension is not part of the URI when you're trying to match routes. Typically, URIs don't include the .php extension.

Here's an updated version of your routing logic that should work if your controllers are directly inside a directory named controllers:

$uri = trim($_SERVER['REQUEST_URI'], '/');

if ($uri === '') {
    require __DIR__ . '/controllers/index.php';
} elseif ($uri === 'about') {
    require __DIR__ . '/controllers/about.php';
} elseif ($uri === 'contact') {
    require __DIR__ . '/controllers/contact.php';
} elseif ($uri === 'blog') {
    require __DIR__ . '/controllers/blog.php';
} else {
    echo 'Gibberish';
}

Here's what I changed:

  • I used trim() to remove any leading or trailing slashes from the URI, which can cause mismatches in the routing conditions.
  • I removed the .php extension from the URIs in the condition checks. This assumes that your .htaccess file or web server configuration is set up to rewrite URLs to remove the .php extension.
  • I added __DIR__ to the require statements to ensure that the path is relative to the current directory of the script. This is important if you're running the script from a different directory.

Make sure that your web server is configured to rewrite URLs so that they don't require the .php extension. If you're using Apache, you might have an .htaccess file with rewrite rules similar to this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ .php [L,QSA]

This rewrite rule will internally append .php to the URI before it's passed to your script, allowing you to omit the .php extension in your routing logic.

If you're still having trouble, double-check the following:

  • The directory structure is correct and the files are in the expected locations.
  • File permissions are set correctly, allowing the web server to read the controller files.
  • The web server's rewrite rules are correctly configured and enabled.

If you've checked all of these and are still facing issues, you may need to provide more information about your setup for a more specific solution.

emanjbollie79's avatar

I refactored the code you suggested, and it just made matters worse on XAMPP.

emanjbollie79's avatar

now i get this error: Secure Connection Failed

An error occurred during a connection to localhost. SSL received a record that exceeded the maximum permissible length.

Error code: SSL_ERROR_RX_RECORD_TOO_LONG

The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
Please contact the website owners to inform them of this problem.

Please or to participate in this conversation.