I’m currently going through Laracast’s PHP for Beginners series, and in episode 30, they make a change to the document root. I’m using XAMPP on Windows, and the default document root in my httpd.conf was C:/xampp/htdocs
. I’ve changed this to my desired document root, C:/xampp/htdocs/public
, but only the public/index.php
page works and nothing else.
I seem to receive a 404 when trying to access any file/page that’s outside the /public
folder, which makes sense. However, they watching the video, they seem to be able to.
https://www.youtube.com/watch?v=cayyhoFnqSY&list=PL3VM-unCzF8ipG50KDjnzhugceoSG3RTC&index=30
My file structure is as follows:
controllers
notes
create.php
index.php
index.php
public
index.php
views
notes
create.view.php
index.view.php
partials
banner.php
footer.php
header.php
nav.php
404.php
index.view.php
config.php
Database.php
functions.php
router.php
routes.php
Validator.php
My current httpd.conf (Line 252 & 253):
DocumentRoot "C:/xampp/htdocs/public"
<Directory "C:/xampp/htdocs/public">
I’ve also tried the following in httpd-vhosts.conf instead (https://serverfault.com/questions/917029/apache-server-wont-change-root-directory):
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "C:/xampp/htdocs/public"
ServerName localhost
<Directory "C:/xampp/htdocs/public">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
My .htaccess file (tried commenting these rules out, in case they caused issues):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
My router.php file:
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$routes = [
'/' => 'controllers/index.php',
'/notes' => 'controllers/notes/index.php',
'/notes/create' => 'controllers/notes/create.php',
];
function routeToController($uri, $routes) {
if (array_key_exists($uri, $routes)) {
require $routes[$uri];
} else {
abort();
}
}