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

Posted on June 08, 2015

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. Apr 28, 2015 | laravel, laravel 5.1
    Taylor's put up a new Laracast video about using the new Broadcasting tool in Laravel 5.1, which is a simple contract that al...
  2. Jun 8, 2015 | laravel, laravel 5.1
    Laravel 5.1 introduces "Service Injection", the ability to "inject" an object into Blade templates. This ...
  3. Jun 9, 2015 | laravel, laravel 5.1
    If you remember my blog post about Filter-style middleware in Laravel 5.0, middleware got a big bump in its importance and pr...
  4. Jun 10, 2015 | laravel, laravel 5.1, artisan
    If you're not familiar with Artisan commands, they're command-line functions that you can run to interact with your Laravel a...
  5. Jun 11, 2015 | laravel, laravel 5.1, artisan, console
    Yesterday we talked about the new signature syntax for Artisan commands. Today, let's take a look at the options Artisan comm...
  6. Jun 15, 2015 | laravel, laravel 5.1, testing, integration testing
    Whether or not you've written integration tests in Laravel before, you'll be pleased to hear that integration testing is more...
  7. Jun 16, 2015 | laravel, laravel 5.1, testing, integration testing
    When you're testing your code, you often want to create a fake entry for one or more of your models. You may have tried this ...
  8. Jun 19, 2015 | laravel, laravel 5.1, testing, integration testing
    Continuing in our series about testing and integration testing in Laravel 5.1, let's take a look at some new traits that are ...
  9. Jul 7, 2015 | laravel, laravel 5.1
    Whoops! I missed a feature from my Laravel 5.1 review! It's now super easy to exclude specific routes from your CSRF middlewa...
  10. Jul 31, 2015 | laravel, laravel 5.1
    If you've ever run a SaaS (or put any web site with comments or signups on the Internet for any length of time), you've exper...
  11. Sep 9, 2015 | acl, laravel, laravel 5.1, authorization
    The authentication that Laravel provides out-of-the-box makes it simple to get user signup, login, logout, and password reset...

Subscribe

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