I’m trying to build my own MVC as a practice and learning experience. So far, this is what I have (index.php):
<?php
require "config.php";
$page = $_GET['page'];
if( isset( $page ) ) {
if( file_exists( MVCROOT . "/$page.php" ) ) {
include "$page.php";
} else {
header("HTTP/1.0 404 Not Found");
}
}
?>
My problem here is, I can’t use header to send to a 404 because the headers have been sent already. Should I just redirect to a 404.html or is there a better way? Feel free to critique what I have so far (it’s very little). I would love suggestions and ideas. Thanks!
Standard practice in MVC frameworks is to use output buffering (
ob_start(),ob_get_contents(), andob_end_clean()) to control how, when, and what gets sent to the user.This way, as long as you capture your framework’s output, it doesn’t get sent to the user until you want it to.
To load the 404, you would use (for example):
Hope that helps.