If you’ve worked with Laravel, you’re probably familiar with its templating engine, Blade, which is used for rendering views. However, you don’t need to use Laravel in its entirety to take advantage of this powerful view rendering system.
You can use the illuminate/view package in standalone PHP projects to render views, just like you would in a Laravel app.
In this guide, we’ll walk you through setting up illuminate/view in a simple PHP script to render views using the same templating engine that Laravel uses.
Why Use illuminate/view?
The illuminate/view package is a standalone component from Laravel that allows you to:
- Render dynamic HTML views.
- Use Blade templating to manage your views.
- Organize your templates more easily.
This is perfect if you want to keep your project lightweight and don’t want to use the full Laravel framework, but still need a powerful templating system.
Steps to Use illuminate/view in Standalone PHP
Let’s walk through the steps to use illuminate/view outside of Laravel.
Step 1: Install the Required Packages via Composer
First, you need to install the illuminate/view package. It requires some dependencies to work properly, so we’ll also need to install illuminate/filesystem (for file handling) and illuminate/support (which includes helper functions).
Run the following command in your project’s root directory:
composer require illuminate/view illuminate/filesystem illuminate/support
This will download the necessary packages.
Step 2: Set Up a Simple PHP Script
Now, let’s create a basic PHP script that uses the illuminate/view package to render a simple view file.
Folder Structure
Here’s a simple folder structure for your project:
/your-project
/views
home.blade.php
index.php
composer.json
vendor/
1. Create the Blade Template (home.blade.php)
In the /views directory, create a simple Blade template called home.blade.php.
<!-- /views/home.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Blade View</title>
</head>
<body>
<h1>Hello, {{ $name }}!</h1>
<p>Welcome to your first Blade view rendered outside of Laravel.</p>
</body>
</html>
In this Blade template, we are displaying a variable called {{ $name }}. This will be passed from the PHP script.
2. Create the PHP Script (index.php)
Now, let’s create the index.php script that will render the Blade view.
<?php
require_once 'vendor/autoload.php';
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
echo 'hello from view file';
// Set up filesystem and cache path
$filesystem = new Filesystem();
$cachePath = __DIR__ . '/cache';
// Set up Blade compiler
$bladeCompiler = new BladeCompiler($filesystem, $cachePath);
// Set up engine resolver and register the Blade engine
$engineResolver = new EngineResolver();
$engineResolver->register('blade', function () use ($bladeCompiler) {
return new CompilerEngine($bladeCompiler);
});
// Set up view finder
$viewPaths = [__DIR__ . '/views'];
$viewFinder = new FileViewFinder($filesystem, $viewPaths);
// Set up event dispatcher
$eventDispatcher = new Dispatcher(new Container());
// Set up the view factory
$viewFactory = new Factory($engineResolver, $viewFinder, $eventDispatcher);
// Data to pass to the view
$data = ['name' => 'John Doe'];
// Render the view (home.blade.php) and pass data to it
echo $viewFactory->make('home', $data)->render();
Explanation of the Code:
- Autoloading:
- We include Composer’s autoloader with
require 'vendor/autoload.php';so we can use all the installed packages.
- We include Composer’s autoloader with
- Set Up Filesystem and BladeCompiler:
Filesystem: This handles file operations (like reading view files).BladeCompiler: This compiles Blade templates into plain PHP. We pass it a cache directory (__DIR__ . '/cache') where the compiled Blade files will be stored.
- Set Up View Factory:
- The
Factoryclass is responsible for rendering views. We pass theBladeCompilerandFilesystemto the factory. - We add the path where our Blade views are stored (
$viewPath).
- The
- Render the View:
- We define an associative array,
$data, which contains the variables we want to pass to the Blade view. In this case, we passnamewith the value “John Doe”. - We use the
make()method to create a view instance and pass the data. Finally, we callrender()to output the view.
- We define an associative array,
3. Run the Script
Once everything is set up, you can open your index.php in a web browser. You should see the rendered Blade view, displaying something like:
Hello, John Doe!
Welcome to your first Blade view rendered outside of Laravel.
What’s Happening Behind the Scenes?
- BladeCompiler:
- The
BladeCompilerclass is responsible for compiling the Blade templates (the.blade.phpfiles) into pure PHP. Once compiled, it stores the compiled files in the cache directory (/cache). - The Blade engine processes things like
{{ $name }}and converts them into valid PHP code that outputs the correct values.
- The
- View Factory:
- The
Factoryclass is used to create the view and pass data to it. The factory handles things like loading the correct template file, passing data, and rendering the output.
- The
- Files:
- The
Filesystemclass is used to interact with the file system. It reads view files from theviews/directory and writes the compiled Blade files to the cache.
- The
Customizing the View Rendering
You can also use layouts, partials, and other Blade features when using illuminate/view outside of Laravel. For example, if you wanted to use a layout, you could structure your views like this:
Layout View (layouts/app.blade.php):
<!-- /views/layouts/app.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title', 'My App')</title>
</head>
<body>
<header>
<h1>Welcome to My App</h1>
</header>
<div class="content">
@yield('content')
</div>
<footer>
<p>© 2025 My App</p>
</footer>
</body>
</html>
Child View (home.blade.php):
<!-- /views/home.blade.php -->
@extends('layouts.app')
@section('title', 'Home Page')
@section('content')
<h2>Hello, {{ $name }}!</h2>
<p>Welcome to the home page of your app.</p>
@endsection
In this case, the home.blade.php view will be rendered inside the layouts/app.blade.php layout, and you can use the @yield and @section directives to define and extend sections of your layout.
Conclusion
With the illuminate/view package, you can easily integrate Blade templating into your standalone PHP projects. This approach allows you to use powerful templating features like Blade, layouts, and views without needing to install the full Laravel framework.
By following the steps above, you can set up a clean and efficient view rendering system in your PHP scripts using Laravel’s templating engine!
