0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Laravel】サービスプロバイダ

Posted at

サービスプロバイダとは

サービスを提供するための仕組みです。
サービスプロバイダのクラスは下記のように定義されています。

app/providers/サービスプロバイダ
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;


class プロバイダクラス名 extends ServiceProvider
{
    public function register()
    {
        //登録時に実行される処理
    }

    public function boot()
    {
        //アプリケーションの起動時に実行される処理
    }
}

registerメソッドは登録時に処理される関数です。
bootメソッドはアプリケーションを起動した時に実行される処理です。

作成から利用するための登録までの概要を説明します。

サービスプロバイダ作成

下記のコマンドでサービスプロバイダを作成します

ターミナル
php artisan make:provider RiakServiceProvider

上記のコマンドで作成されたファイルが下記のようになります。

app/providers/RiakServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RiakServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

registerboot内にそれぞれ必要な処理を記述してください

プロバイダの登録

上記で編集したサービスプロバイダを利用するためには登録する必要があります。
登録するためにconfig/app.phpを編集します。

config/app.php
'providers' => [
    // Other Service Providers

    App\Providers\RiakServiceProvider::class,
],

下記のように追記してください。

以上で、サービスプロバイダの使い方の概要になります。

疑問、気になるところがございましたら、質問、コメントよろしくお願いします!!!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?