Skip to content

Instantly share code, notes, and snippets.

@rosswintle
Created May 17, 2017 08:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rosswintle/428ae8a3e0c215a7b3ecbfd186b5520c to your computer and use it in GitHub Desktop.
Save rosswintle/428ae8a3e0c215a7b3ecbfd186b5520c to your computer and use it in GitHub Desktop.
Simple Laravel Service Container/Provider example
<!-- A Blade component that uses the stats -->
<section class="section stats">
<h3 class="title">
How many things we made and did...
</h3>
<p><strong>Today:</strong> {{ $stats->today }}</p>
<p><strong>Yesterday:</strong> {{ $stats->yesterday }}</p>
<p><strong>This Month:</strong> {{ $stats->month }}</p>
<p><strong>This Year:</strong> {{ $stats->year }}</p>
</section>
<?php
// This is the Stats class I wanted to create. It lives in app/Stats.php, but could be anywhere that the AutoLoader will recognise it
namespace App;
use App\Action;
use Carbon\Carbon;
/**
*
*/
class Stats
{
private $now;
public $today = 0;
public $yesterday = 0;
public $week = 0;
public $month = 0;
public $year = 0;
function __construct()
{
$this->now = Carbon::now();
var_dump($this->now);
$this->constructToday();
$this->constructYesterday();
$this->constructMonth();
$this->constructYear();
}
function constructToday() {
$this->today = Action::whereDate( 'created_at', $this->now->toDateString() )->count();
}
function constructYesterday() {
$yesterday = Carbon::yesterday();
$this->yesterday = Action::whereDate( 'created_at', $yesterday->toDateString() )->count();
}
function constructMonth() {
$this->month = Action::whereMonth( 'created_at', $this->now->month )->count();
}
function constructYear() {
$this->year = Action::whereYear( 'created_at', $this->now->year )->count();
}
}
<?php
// This is the service provider that instantiates the Stats class and shoves it into the service container.
// This file lives in app/Providers/StatsServiceProvider.php and was scaffolded using 'php artisan make:provider'
// This needs to be registered in config/app.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use App\Stats;
class StatsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->app->singleton( Stats::class, function () {
return new Stats();
});
View::share('stats', app('App\Stats'));
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
@Lexiebkm
Copy link

Lexiebkm commented Jan 10, 2019

Thanks. This helps answer my question concerning the right location for placing our service container. I thought it could only be placed in the default service provider, i.e AppServiceProvider, because the official documentation doesn't provide example for the location.
However, you should place code for service container in the register method, not boot method : https://laravel.com/docs/5.7/providers#the-register-method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment