Using the Laravel Blade templating engine in Codeigniter 3

As anyone who reads this blog or knows me personally, my love for Laravel is deep. That’s great for greenfield projects but a lot of code I deal with on a daily basis is mostly written in Codeigniter. One thing I really miss in CI (among many other things) is a template system. In this tutorial I will show you how to include the Laravel Blade template system in your Codeigniter project.

The first thing we need to do is get our project up and running so grab your self a copy of Codeigniter and set it up so you can view it locally.

Now its running, run the built in composer.json file:

composer install

This will run the default composer file that comes with Codeigniter, nothing special or new here. Now lets grab the blade package from duncan3dc:

composer require duncan3dc/blade

This will install the package and all its dependencies automatically for us. Great!

For some crazy reason, Codeigniter decided to create their own “autoloader” which is actually not an autoloader at all, its actually just Eager-loading everything put into an array. So lets get rid of this poor decision and use the composer autoloader instead. Open application/config/config.php and change the composer_autoload option from FALSE to TRUE;

$config['composer_autoload'] = TRUE;

Okay if you try and use a package at this moment, it won’t work, composer wont be able to find it, huh whats going on? Step up crazy decision number 2 (I’d love to know who on the Codigniter team is coming up with these mad decisions). The default location for the composer autoloader.php is set to application/vendor/autoload.php but the composer.json file is set-up to put the vendor folder in the root of the application?! Crazy decisions all over this framework so lets change this default. Still in the application/config/config.php file set the composer_autoload config to the following instead:

$config['composer_autoload'] = __DIR__ . '/../../vendor/autoload.php';

So lets open our Welcome.php controller and include the blade template package. At the top of the file under the define base path line, let the class know you want to use the blade package:

use duncan3dc\Laravel\BladeInstance;

Now inside your index method create a new blade instance and tell it to render the index view. When you create a new blade instance you need to pass it the path to your views folder and cache folder.  If you have your views in a different location then you need to change the path below to match.

$blade = new BladeInstance(__DIR__ . "/../views", __DIR__ . "/../cache/views");

echo $blade->render("index");

Create the index.blade.php in the root of your views folder and simply drop some HTML in for now to test its working:

<h1>Hello Blade</h1>

Great work! You now have blade up and running.

Two things to note here:

1) You will probably want to extract this away into your parent class so you don’t have to create a new blade instance inside every single controller.

2) It will be best to place the path to your views and cache folder inside your config array rather than passing a hard coded string into the blade constructor.

Lets address point 1, create a new controller in /application/core/MY_Controller.php. Note you must prefix your controller with MY_ I know this seems stupid but its yet another great architecture decision by the codeigniter team. Type the following code into that file:

use duncan3dc\Laravel\BladeInstance;

class MY_Controller extends CI_Controller
    protected $blade;

    function __construct()
        $this->blade = new BladeInstance(__DIR__ . "/../views", __DIR__ . "/../cache/views");

Nothing new going on here, we create a protected property and in the constructor assign this property to a new blade instance. Now any controller that extends our new controller will have access to that blade instance, so lets try it in our welcome controller:

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends MY_Controller {
    public function __construct() 
    public function index()
        $data = array('title' => 'Blade Title');
        echo $this->blade->render("index", $data);

This is now looking much cleaner, we extend our new controller

class Welcome extends MY_Controller

so now we have access to the $balde property in our parent controller. We can now call this property from our index method

echo $this->blade->render("index", $data);

2 thoughts on “Using the Laravel Blade templating engine in Codeigniter 3”

  1. Hi Mark,
    I’m getting the following error when using @include:

    Type: ErrorException

    Message: Engine php not found. (View: /projects/ditto/www/application/views/communication/index.blade.php)

    Filename: /projects/ditto/vendor/illuminate/view/Engines/EngineResolver.php

    Line Number: 57

    Any idea what this might be? Composer in not installed in the project root, but in a separate `www` folder. So vendor and composer are on the same level, as opposed to vendor being inside the codeigniter folder. Could this be the issue?

    1. Sorry, I have “resolved” the issue. I’m not used to the switch yet, the file I was including was missing .blade.

      Otherwise thanks for the integration! Especially happy it runs with Composer.


Leave a Reply

Your email address will not be published. Required fields are marked *