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:
- Make sure that the directory you moved your controllers into is correctly referenced in your routing logic.
- Ensure that the
.phpextension is not part of the URI when you're trying to match routes. Typically, URIs don't include the.phpextension.
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
.phpextension from the URIs in the condition checks. This assumes that your.htaccessfile or web server configuration is set up to rewrite URLs to remove the.phpextension. - I added
__DIR__to therequirestatements 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.