Download the PHP package lambertns/laravel-make-service without Composer
On this page you can find all versions of the php package lambertns/laravel-make-service. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lambertns/laravel-make-service
More information about lambertns/laravel-make-service
Files in lambertns/laravel-make-service
Package laravel-make-service
Short Description Adds `php artisan make:service` command to generate service classes with optional automatic controller injection using PHP 8+ property promotion.
License MIT
Informations about the package laravel-make-service
Laravel Make Service
A Laravel package that adds a make:service command to generate service classes with optional dependency injection into controllers.
Why Use Services?
Controllers can quickly become bloated with business logic, making them hard to maintain and test. Services help by:
- ๐ง Separate Concerns: Move business logic out of controllers into dedicated service classes
- ๐ Reduce Controller Complexity: Keep controllers thin and focused on HTTP handling
- โป๏ธ Reusability: Share business logic across multiple controllers or queue jobs
- ๐งช Testability: Easier to unit test business logic in isolation
- ๐ Maintainability: Organized code that's easier to read and maintain
Example Problem
Without Services (Fat Controller):
With Services (Clean Controller):
Features
- ๐ Generate service classes with the
php artisan make:servicecommand - ๐ Automatically inject services into controllers using the
--controlleroption - ๐ Use PHP 8+ constructor property promotion for clean dependency injection
- ๐ฏ Add multiple methods to services using the
--methodsoption - โ Follows Laravel conventions and best practices
Installation
You can install the package via Composer:
That's it! The package will automatically register the service provider.
Quick Start
Let's say you have a controller with lots of business logic and want to refactor it:
-
Create a service to extract the logic:
-
Your service is created and automatically injected:
- Use it in your controller:
Usage
Basic Service Creation
Create a simple service:
This will create app/Http/Services/UserService.php:
Create Service with Methods
Add methods to your service:
This creates a service with three methods:
Auto-Inject into Controller
Automatically inject the service into a controller:
This will:
- Create
app/Http/Services/PaymentService.php - Add
use App\Http\Services\PaymentService;to the controller - Add constructor injection with property promotion:
Inject into Multiple Controllers
Inject the service into multiple controllers at once (comma-separated):
This will inject the service into all three controllers and show a summary:
Inject into Existing Controller
You can also add services to controllers that already have dependencies:
If the controller already has a constructor with dependencies, the new service will be added:
Command Options
| Option | Description | Example |
|---|---|---|
--controller |
Controller(s) to inject into (comma-separated for multiple) | --controller=AuthController,UserController |
--methods |
Comma-separated list of method names | --methods="index,store,update" |
--force |
Overwrite existing service | --force |
Examples
Create a service with multiple methods and inject into multiple controllers:
This will create the OrderService with three methods and inject it into both controllers.
Use in nested namespaces:
Requirements
- PHP >= 8.1
- Laravel >= 10.0
- Illuminate/Support >= 10.0
- Illuminate/Console >= 10.0
- Illuminate/Filesystem >= 10.0
Features
Constructor Property Promotion
This package uses PHP 8's constructor property promotion for clean dependency injection:
This eliminates the need for separate property declarations and manual assignment.
Smart Use Statement Insertion
The package intelligently adds use statements in the correct location:
Troubleshooting
Service not being injected?
- Make sure the controller exists
- Check that the controller has a valid namespace
- Verify the controller file is writable
License
This package is open-sourced software licensed under the MIT license.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
All versions of laravel-make-service with dependencies
illuminate/support Version >=10.0
illuminate/console Version >=10.0
illuminate/filesystem Version >=10.0