Today, I challenged myself to implement a Laravel MVC (Model, View, Controller) approach using pure vanilla PHP. To tackle this, I utilized the standard Laravel Blade templating engine for the view part of the project, but without installing Laravel since my project is purely vanilla PHP. Hereโs how I achieved this integration;
I started by installing a package called Blade by JensSegers using the following Composer command:
composer require jenssegers/blade
This package allows the Blade templating engine to be used as a standalone package, making it compatible with any PHP project, not just Laravel.
Next, I ran the following command to update the illuminate/view dependency of the package to version 11.7.0, as the package does not work correctly with versions below 11.7.0:
composer require illuminate/view:11.7.0
Then, I created a database called "pdotest" with a table called "post," which has columns "name" and "body." I populated this table with data ('this is post name from database displayed using Blade template engine', 'this is post body from database displayed using Blade template engine') respectively and connected to it by creating Database.php file in root of my project and put following code:
<?php
class Database {
private $host = "localhost";
private $database ="pdotest";
private $username = "root" ;
private $password = '';
public function connect(){
try {
$conn = new PDO("mysql:host=$this->host;dbname=$this->database",$this->username,$this->password);
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// $conn = $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
}
?>
Next, I created a directory called "Models" for all database manipulations. Inside this directory, I created a file named Post.php to handle operations on the "post" table. Within this file, I defined a method called post to retrieve a post by its ID, as shown below:
<?php
require './Database.php';
class Post {
public $conn;
private $table="post";
public function __construct() {
$this->conn = (new Database)->connect(); // Access directly (less secure)
}
public function getPost($id){
$stmt= "SELECT * FROM $this->table WHERE id = :id";
$stmt = $this->conn->prepare($stmt);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $result = $stmt->fetch();
}
}
?>
Next, I created index.php to act as a router, connecting my controller, view, and model. Inside it, I imported all my views and cache paths, which I will create for placing my Blade views and cache files. These paths were then passed to the Blade class, which comes with the Blade package we installed at the beginning. I then passed this Blade instance to the PostController class, which I will create, and called the post method of that class to get a post by its ID in the future.
<?php
require __DIR__ . '/vendor/autoload.php';
use Jenssegers\Blade\Blade;
require 'Controllers/ControllerPost.php';
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
if (!is_dir($cache)) {
mkdir($cache, 0755, true);
}
$blade = new Blade($views, $cache);
// Simulate routing (In a real application, use a routing library)
$controller = new PostController($blade);
$controller->post();
?>
Then, I created a "controllers" directory at the root of my project. Inside it, I received the Blade variable from index.php (the router), called the getPost method of the Post model to get a post by its ID, and passed that post to a Blade view called "homepage."
<?php
require 'Models/Post.php';
class PostController{
protected $blade;
public function __construct($blade) {
$this->blade = $blade;
}
public function post(){
$post = (new Post)->getPost(1);
echo $this->blade->render('homepage', ['post' => $post]);
}
}
?>
Then, I created a directory called "views" and a file named homepage.blade.php at the root of the project. Inside this file, I simply displayed the name of the post passed from the PostController, as shown below:
<h1>{{$post['name']}}</h1>
Finally, I created a "cache" directory in the root of my project to store Blade cache files, which helps to improve the performance of my project.
Now, when you visit http://localhost/laravel-blade-without-laravel/index.php in your browser, you will see the name of the post displayed as "this is post name from database displayed using Blade template engine."
Thank you for following along with this post on using the Laravel Blade templating engine in a pure vanilla PHP project. I hope it helps you in your development journey. My name is Waziri Ally Amiri, a web developer from Moshi, Tanzania. I specialize in working with Laravel and love to help others get started with this powerful framework. Feel free to reach out if you have any questions or need further assistance.
Top comments (0)