"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Use Laravel Blade Templating Engine with a Pure Vanilla PHP Project?

How to Use Laravel Blade Templating Engine with a Pure Vanilla PHP Project?

Published on 2024-07-31
Browse:789

How to Use Laravel Blade Templating Engine with a Pure Vanilla PHP Project?

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:

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:

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.

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."

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:

{{$post['name']}}

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.

Release Statement This article is reproduced at: https://dev.to/waziridev/how-to-use-laravel-blade-templating-engine-with-a-pure-vanilla-php-project-5h1a?1 If there is any infringement, please contact study_golang @163.comdelete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3