tharindu / facade_generator
A Laravel package to generate facades and services.
Requires
- php: >=8.1
- illuminate/support: ^10.0|^11.0
- phpunit/phpunit: ^9.0|^10.0|^11.0
README
Laravel Domain Generator is a package that provides a convenient way to generate domain services and facades in your Laravel application.
Installation
You can install the package via Composer:
composer require tharindu/facade_generator
Usage
To generate a domain service and facade, use the make:domain
Artisan command:
php artisan make:domain User
This command will create the following files in the domain
directory:
ServiceNameFacade.php
ServiceNameService.php
You can then use these files to implement your domain logic.
Autoloading the Domain Directory
To ensure Laravel autoloads your custom domain
directory, you need to update the composer.json
file of your Laravel application. Follow these steps:
-
Open your Laravel application's
composer.json
file. -
Locate the
autoload
section. It should look something like this:"autoload": { "psr-4": { "App\\": "app/" }, "classmap": [ "database/seeds", "database/factories" ] },
-
Add your
domain
directory to the PSR-4 autoloading section. If your domain directory is located at the root of your Laravel application, you can add it like this:"autoload": { "psr-4": { "App\\": "app/", "Domain\\": "domain/" }, "classmap": [ "database/seeds", "database/factories" ] },
Make sure to adjust the path
"Domain\\"
and"domain/"
according to the actual structure of your application. -
Run
composer dump-autoload
to regenerate the Composer autoloader files:composer dump-autoload
Once you've made these changes, Laravel will autoload classes from your domain
directory just like it does with the app
directory, making your domain logic easily accessible throughout your application.
Functionalities
The package provides the following functionalities:
- Generation of Domain Service and Facade: Easily create domain services and facades using the
make:domain
command. - CRUD Functionality: The generated service stubs include basic CRUD operations for managing domain entities.
- Create: Create new domain entities.
- Read: Retrieve specific domain entities.
- Update: Update existing domain entities.
- Delete: Delete domain entities.
- List: Retrieve a list of domain entities.
Examples
Generating a Domain
To generate a domain service and facade named User
, run:
php artisan make:domain User
This will create UserFacade.php
and UserService.php
in the domain
directory.
Using the Generated Files
Once the files are generated, you can implement your domain logic inside the service methods. For example:
<?php namespace Domain\Services\UserService; use App\Models\User; class UserService { protected $user; public function __construct() { $this->user = new User(); } public function get(int $user_id) { return $this->user->find($user_id); } public function create(array $data) { return $this->user->create($data); } public function read($id) { // Implement read functionality } protected function edit(User $user, array $data) { return array_merge($user->toArray(), $data); } public function update($user_id , array $data) { $user = $this->user->find($user_id); return $user->update($this->edit($user, $data)); } public function delete($id) { $user = $this->user->find($id); return $user->delete(); } public function list() { return $this->user->all(); } }
Contributing
Contributions are welcome! Please feel free to submit a pull request.
License
The Laravel Domain Generator is open-sourced software licensed under the MIT license.