zxf5115/laravel-kernel-service
Laravel package that turns your app kernel into a service layer, helping organize core business logic behind a clean interface. Aims to centralize shared operations, keep controllers thin, and improve maintainability across Laravel projects.
Installation
composer require zxf5115/laravel-kernel-service
Publish the config file (if available):
php artisan vendor:publish --provider="Zxf5115\KernelService\KernelServiceServiceProvider"
First Use Case: Registering a Kernel Service
Define a service in config/kernel-service.php:
'services' => [
'example' => [
'class' => \App\Services\ExampleService::class,
'singleton' => true,
'parameters' => [
'config.key' => 'value',
],
],
],
Bind the service in a service provider (e.g., AppServiceProvider):
use Zxf5115\KernelService\Facades\KernelService;
public function boot()
{
KernelService::register('example');
}
Resolve and Use
$service = app('example'); // Or via facade
$result = $service->doSomething();
AppServiceProvider@boot or a dedicated provider.
KernelService::register('serviceName', [
'class' => \App\Services\MyService::class,
'singleton' => true,
'parameters' => ['param1' => 'value1'],
]);
if (config('app.feature_flag')) {
KernelService::register('conditionalService');
}
$service = app()->make('serviceName');
$result = KernelService::call('serviceName', 'method', [$arg1, $arg2]);
config/kernel-service.php:
'default_lifetime' => 'P1D', // Default TTL for non-singleton services
'scopes' => [
'web' => ['example', 'authService'],
],
KernelService::register('logger', [
'class' => \App\Services\Logger::class,
'singleton' => true,
]);
web, api).
KernelService::scope('web')->register('sessionHandler');
$data = KernelService::call('dataService', 'fetch', ['user_id' => 1]);
Circular Dependencies:
ServiceA depends on ServiceB and vice versa, refactor to introduce a third service or use dependency injection carefully.Singleton Misuse:
singleton: false for request-scoped services or clear the container manually:
app()->forgetInstance('serviceName');
Configuration Overrides:
config/kernel-service.php require a config cache rebuild:
php artisan config:cache
'parameters' => [
'api_key' => env('SERVICE_API_KEY'),
],
Facade vs. Container:
\Illuminate\Support\Facades\Facade::clearResolvedInstance('KernelService');
Service Not Found:
config/kernel-service.php or via KernelService::register().APP_DEBUG=true) to see binding errors.Dependency Injection Failures:
app()->bindings to inspect registered services:
dd(app()->bindings());
parameters array or are resolvable by the container.Custom Resolvers:
KernelService::extend('custom', function ($app, $parameters) {
return new \App\Services\CustomService($parameters['param']);
});
KernelService::resolver('custom', \App\Resolvers\CustomResolver::class);
Event Listeners:
KernelService::registering(function ($name, $config) {
Log::info("Registering service: {$name}");
});
Middleware Integration:
KernelService::scope('web')->register('requestService');
public function handle($request, Closure $next) {
$service = app('requestService');
return $next($request);
}
singleton: true and relying on Laravel’s lazy loading:
KernelService::register('lazyService', [
'class' => \App\Services\LazyService::class,
'singleton' => false,
]);
How can I help you explore Laravel packages today?