Simple MVC Framework With PHP (View)

Chibuzo Miracle
3 min readAug 23, 2022

--

Part 1: Simple MVC Framework With PHP

Part 2: Simple MVC Framework With PHP (bootstrapping)

Part 3: Simple MVC Framework With PHP (Routing)

Here we will carefully develop a view engine that will find, and render our view files located in our view directory.

The aim is to achieve something like below

return view('home.index', compact('first', 'second'))
or
return view ('home.index', ['first', 'second'])

As you must have noticed, this is a familiar syntax for laravel developers, let’s se how we can replicate ours.

Step 1: Create a View.php trait inside your bootstrap folder.

Place the below code inside it:

<?php


namespace Core;


trait View
{
/**
*
@throws \Exception
*/
public function render($view, $params = [])
{
$position = strpos($view, ".");
if ($position){
$view = str_replace(".", "/", $view);
}

if (is_readable(APP_ROOT."/views/$view.php"))
{
return $this->generateView($view, $params);
}else{
throw new \Exception("404 PAGE NOT FOUND", 404);
}
}

/**
*
@throws \Exception
*/
private function generateView($view, $params)
{
foreach ($params as $key => $value){
$$key = $value;
}

ob_start();
require_once APP_ROOT."/views/$view.php";
echo ob_get_clean();
return true;
}
}

The above trait has two methods, the render and generateView function.

The render view is actually what we would be calling directly, the generate function will be responsible for buffering (output), after which we call our ob_get_clean() to return the content of the buffering and delete it from the buffer, so we would not be dealing with a big chunk of memory space.

Step 2: create a helpers folder inside your bootstrap folder (directory), create a new php file called app.php. This PHP file will be responsible for shortening long method calls for the framework.

Inside this PHP file, paste the following code:

<?php
$app = new \Core\Framework();
/**
*
@throws Exception
*/
function view($view, $params = [])
{
global $app;
return $app->render($view, $params);
}

In the above code, we assigned our core (Framework) class a variable, and accessed it as a global in our functions, this way we have access to the view trait.

Step 3: add this line of code to your composer.json, this is an instruction to autoload this file anytime the application is run.

"files": ["bootstrap/helpers/app.php"]

Your composer.json would look something like this:

{
"name": "emiracle/simple-mvc-framework-with-php",
"description": "Simple MVC framework with PHP",
"autoload": {
"psr-4": {
"App\\": "app/",
"Core\\": "bootstrap/"
},
"files": ["bootstrap/helpers/app.php"]
}
}

Step 4: run “composer dump-autoload”

Step 5: Let’s create a home directory inside our views folder, and in the home folder add an index.php file in it.

<!DOCTYPE html>
<html>
<body>

<h1>My First Framework</h1>

</body>
</html>

Paste the above code inside the views/home/index.php.

Now edit your HomeController, in the index method return the view instead of an echo. It should look something like this:

<?php
namespace App\Controllers;

class HomeController
{
public function test()
{
return view('home.index');
}
}

If you visit the homepage of your application you should see something like this on your browser:

Now proceed to pass arguments into the view function like this:

public function test()
{
$string = "My First Framework";
return view('home.index', compact('string'));
}

The response on the browser should be same.

Thanks for staying with me until now!

Look out for ORM, Request passing and other interesting stuffs!

NB: This is strictly for educational purposes and as such is not recommended to be used in production.

Part 5: Simple MVC Framework With PHP (Request Parsing)

--

--

Chibuzo Miracle

B Eng A&B Engineering || Software Developer || Technical Writer