😅😒 ServiceProvider cho local, Debugbar sử dụng auto-discovery, composer install --no-dev (ok)

https://github.com/svenluijten/artisan-view

composer install --no-dev loại bỏ dev đã dùng :) ví dụ xóa Debugbar

C:\xampp8\htdocs\plugindev\app\Providers\AppServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    if ($this->app->environment() !== 'production') {
      $this->app->register(\Sven\ArtisanView\ServiceProvider::class);
    }
  }
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    //
  }
}

Laravel Package Auto-Discovery

Go out and find a few Laravel packages and read skim through each one’s readme file and see if you notice anything in common. I’ll give you a hint; every Laravel Package outlines the following steps:

Install the package:

composer require foo/bar

Register the provider in app.php:

Foo\Bar\ServiceProvider::class,

Optionally, register the Facade:

'Bar' => Foo\Bar\Facade::class,

The steps are far from difficult, but it’s always a requirement, and in the past, people have to get around this by releasing their own package installer, but it never stuck. Probably because it wasn’t directly supported by the framework.

All this is changing and coming in Laravel 5.5 is a brand new way for packages to register their service providers and aliases in the package composer.json file. This saves us the hassle of having to go through all those steps above.

If you are a package developer you can see Taylor’s post on it and see this pull request that was submitted to DebugBar that demonstrates how to set it up or add this to your composer.json:

"extra": {
    "laravel": {
        "providers": [
            "Foo\\Bar\\ServiceProvider"
        ],
        "aliases": {
            "Bar": "Foo\\Bar\\Facade"
        }
    }
}

Package auto-discovery is a small change, but it will make installing packages easier and more streamlined than ever before.

Last updated