win-aung/laravel-entity-generator
Composer 安装命令:
composer require win-aung/laravel-entity-generator
包简介
A Laravel package to generate CRUD entities with controllers, models, services, requests, resources, and migrations using service design pattern
README 文档
README
A powerful Laravel package that generates complete CRUD entities with controllers, models, services, requests, resources, and migrations using the service design pattern.
Features
- 🚀 Fast Entity Generation: Generate complete CRUD entities with a single command
- 🏗️ Service Pattern: Built-in service layer for business logic separation
- 📝 Auto-generated Files: Controllers, Models, Services, Requests, Resources, and Migrations
- ⚡ Laravel Best Practices: Follows Laravel conventions and design patterns
- 🔧 Customizable: Easy to customize stubs and configuration
- 💪 Production Ready: Includes proper error handling and transactions
Installation
Via Composer
composer require win-aung/laravel-entity-generator:dev-main
Usage
Basic Usage
Generate a complete entity with all CRUD operations:
php artisan make:entity User
This will create:
app/Services/User/UserService.php- Service layer for business logicapp/Http/Controllers/UserController.php- RESTful controller with CRUD methodsapp/Models/User.php- Eloquent modelapp/Http/Requests/UserRequest.php- Form request validationapp/Http/Resources/UserResource.php- API resource for JSON responsesdatabase/migrations/*_users_table.php- Database migration
Force Overwrite
To overwrite existing files:
php artisan make:entity User --force
Interactive Mode
If files already exist, the command will ask for confirmation:
php artisan make:entity User
# Will prompt: "File already exists: app/Http/Controllers/UserController.php. Overwrite?"
Generated Files Structure
Controller (UserController.php)
<?php namespace App\Http\Controllers; use App\Http\Requests\UserRequest; use App\Services\User\UserService; class UserController extends Controller { protected $userService; public function __construct(UserService $userService) { $this->userService = $userService; } public function index() { return $this->sendResponse($this->userService->getAll(), 200, 'User retrieved'); } public function store(UserRequest $request) { $data = $this->userService->create($request->all()); return $this->sendResponse($data, 201, 'User created'); } // ... other CRUD methods }
Service (UserService.php)
<?php namespace App\Services\User; use App\Models\User; class UserService { protected $model; public function __construct(User $user) { $this->model = $user; } public function getAll() { return $this->model->all(); } public function create($data) { return $this->model->create($data); } // ... other methods }
Configuration
Publish the configuration file to customize the package:
php artisan vendor:publish --tag=config
This will create config/laravel-entity-generator.php with the following options:
return [ 'service_namespace' => 'App\\Services', 'controller_namespace' => 'App\\Http\\Controllers', 'model_namespace' => 'App\\Models', 'request_namespace' => 'App\\Http\\Requests', 'resource_namespace' => 'App\\Http\\Resources', 'generate_repository' => false, 'generate_tests' => false, 'directory_permissions' => 0755, ];
Customization
Custom Stubs
You can customize the generated code by publishing and modifying the stub files:
- Publish the stubs:
php artisan vendor:publish --tag=stubs
-
Modify the stub files in
resources/stubs/ -
Update the configuration to use your custom stubs:
'stub_paths' => [ 'controller' => resource_path('stubs/controller.crud.stub'), 'service' => resource_path('stubs/service.stub'), ],
API Endpoints
The generated controller provides these RESTful endpoints:
GET /users- List all usersPOST /users- Create a new userGET /users/{id}- Show a specific userPUT /users/{id}- Update a userDELETE /users/{id}- Delete a user
Response Format
All API responses follow a consistent format:
Success Response
{
"success": true,
"data": {...},
"message": "User created"
}
Error Response
{
"success": false,
"message": "User not found"
}
Requirements
- PHP >= 8.0
- Laravel >= 9.0
Testing
composer test
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
The MIT License (MIT). Please see License File for more information.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
- Win Aung
- All Contributors
Made with ❤️ for the Laravel community
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-23