Does anyone know how to correctly set up paths in php using an MVC design pattern? I am getting some errors which I am not sure how to resolve when requireing my headers and footers. Here is the error:
Warning: require(C:\laragon\www\erikmvc\app/views/inc/header.php): failed to open stream: No such file or directory in C:\laragon\www\erikmvc\app\views\pages\index.php on line 1
Fatal error: require(): Failed opening required 'C:\laragon\www\erikmvc\app/views/inc/header.php' (include_path='.;C:/laragon/etc/php/pear') in C:\laragon\www\erikmvc\app\views\pages\index.php on line 1
The code is as follows.
config.php
<?php
// App Root (in FILE I have it surrounded by __ and __ , Laragon forum stripped it out of the code).
define('APPROOT', dirname(dirname(FILE)));
// URL Root
define('URLROOT', 'http://erikmvc.test/');
// Site Name
define('SITENAME', 'Erik MVC');
The view load:
// Load the view
public function view($view, $data = []) {
// Check for view file
if(file_exists('../app/views/' . $view . '.php')) {
require_once '../app/views/' . $view . '.php';
} else {
// View does not exist
die("The View Does Not Exist.");
}
}
index.php
<?php require APPROOT . '/views/inc/header.php'; ?>
<h1><?php echo $data['title']; ?></h1>
<?php require APPROOT . '/views/inc/footer.php'; ?>
And finally header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo SITENAME; ?></title>
<link rel="stylesheet" href="<?php echo URLROOT; ?>/css/style.css" type="text/css" media="screen" />
</head>
<body>
If anyone can help out with this, I would sure appreciate it. Thank you.