Jun 8, 2015 | laravel, laravel 5.1

Injecting an object (from the IOC) using Laravel Blade Service Injection

Series

This is a series of posts on New Features in Laravel 5.1.

!
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

Laravel 5.1 introduces "Service Injection", the ability to "inject" an object into Blade templates. This means you can now resolve an object out of the IOC within your templates:

// Blade template
@inject('service', 'App\Services\Service')
{{ $service->getSomething() }}

As you can see, the first parameter is the variable name, and the second parameter is the class or interface name or alias.

NOTE: You don't want to abuse this. There is such a thing as too much logic in your views, and it's a beast. But there are some circumstances in which instantiating a class in every controller just to pass them to the same view is a little bit too much work. Think about a context in which you might want a View Composer, but you only need a single class and binding a full View Composer might seem like too much work.

So, before you might've done this:

// DashboardController
public function index()
{
    return view('dashboard')
        ->with('analytics', App::make('App\Services\Analytics'));
}
// dashboard.blade.php
// Template content...

@include('user.partials.finances-graph', ['analytics' => $analytics])

// Template content...
// UserController
public function showFinances()
{
    return view('user.finances')
        ->with('analytics', App::make('App\Services\Analytics'));
}
// user/finances.blade.php
// Template content...

@include('user.partials.finances-graph', ['analytics' => $analytics])

// Template content...
// user/partials/finances-graph.blade.php
<h3>Finances</h3>

<div class="finances-display">
     {{ $analytics->getBalance() }} / {{ $analytics->getBudget() }}
</div>

As you can see, we have two different controllers, pulling in two different templates, but those templates are both including the same partial template that needs the statistics service.

Let's rework this. Since it's just a single service, we'll inject the service into our template instead of creating a View Composer:

// DashboardController
public function index()
{
    return view('dashboard');
}
// dashboard.blade.php
// Template Content...

@include('user.partials.finances-graph')

// Template Content...
// UserController
public function showFinances()
{
    return view('user.finances');
}
// user/finances.blade.php
// Template Content...

@include('user.partials.finances-graph')

// Template Content...
// user/partials/finances-graph.blade.php
@inject('analytics', 'App\Services\Analytics')

<h3>Finances</h3>

<div class="finances-display">
     {{ $analytics->getBalance() }} / {{ $analytics->getBudget() }}
</div>

That's it! You're now a Service Injection professional.


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  laravel 5.1


This is part of a series of posts on New Features in Laravel 5.1:

  1. Jun 9, 2015 | laravel, laravel 5.1
  2. Jun 15, 2015 | laravel, laravel 5.1, testing, integration testing
  3. Jun 16, 2015 | laravel, laravel 5.1, testing, integration testing
  4. Jul 31, 2015 | laravel, laravel 5.1
  5. Sep 9, 2015 | acl, laravel, laravel 5.1, authorization

Subscribe

For quick links to fresh content, and for more thoughts that don't make it to the blog.