Skip to main content

Service

Generate a new service

You may generate a new service directory by using the following command

php artisan bit.service:make Admin

After the command line executes successfully, a folder named Admin will be created in the path app/Services.

app/
โ””โ”€โ”€ Services/
โ””โ”€โ”€ Admin/
โ”œโ”€โ”€ Features
โ”œโ”€โ”€ Http/
โ”‚ โ”œโ”€โ”€ Controllers
โ”‚ โ””โ”€โ”€ Middlewares
โ”œโ”€โ”€ Providers/
โ”‚ โ”œโ”€โ”€ AdminServiceProvider.php
โ”‚ โ””โ”€โ”€ RouteServiceProvider.php
โ””โ”€โ”€ routes/
โ”œโ”€โ”€ api.php
โ””โ”€โ”€ web.php

Register a service

Services that have been successfully created do not automatically enroll them in your application. So you need to manually register them if you want to use them.

Via configuration

In config/app.php configuration file, you may declare the Service Provider class of the service that you want to register.

config/app.php
'providers' => [
\App\Services\Providers\AdminServiceProvider::class,
]

Via other Service Provider

You may register it in any Service Provider that registered in your application, such as App\Providers\AppServiceProvider.

app/Providers/AppServiceProvider.php
use App\Services\Providers\AdminServiceProvider;

public function register()
{
$this->app->register(AdminServiceProvider::class);
}

With this way, you may register a service dynamically with any condition.

app/Providers/AppServiceProvider.php
use App\Services\Providers\AdminServiceProvider;
use Illuminate\Support\Facades\App;

public function register()
{
if (App::environment('local')) {
$this->app->register(AdminServiceProvider::class);
}
}

List all services

You may see all services in your application via the following command.

php artisan bit.service:list

Delete a service

You may also delete specified service with the given name by the following command.

php artisan bit.service:delete Admin