Repositories Maker
Repository pattern is an abstraction layer for your models., (*1)
Instead of writing tones of duplicated queries in your controllers., (*2)
You can make a repository that has a readable name and implements an interface(for changing the dependencies later) and bind the
repository with it`s interface using the IOC container.
Looks like lots of work to be done ....., (*3)
Repositories Maker makes this process as easy as typing one artisan command., (*4)
1.Go to your laravel project root and type, (*5)
composer require abdelrahmanrafaat
/repositories-maker:dev-master
, (*6)
2.you need to register the package service provider .. Go to config/app.php and add this line to the end of the providers array.
Abdelrahmanrafaat\RepositoriesMaker\Provider\RepositoriesMakerServiceProvider::class
, (*7)
3.This command should add a new command to your artisan list .. make:repositories, (*8)
4.This Command Assumes that your models are at at app directory and extends Model class , but of course you can change this options as i will explain later., (*9)
php artisan make:repositories
, (*10)
This artisan command if you add --help to the end of it you will get the some options., (*11)
-
--parent : Specify the parent class of your models (Only the class base name not the full name space) or you can leave it blank if your models don`t extend a parent class or they extend different parent classes., (*12)
-
--directory : Directory that contains your models starting from the project root directory (this option can`t start with / Or ****)., (*13)
-
--nestedDirectories : boolean option(false by default) indicates if your models are in nested directories., (*14)
-
--except : Comma seperated list of models that you don`t want to generate a repositories for., (*15)
-
--only : create repositories only for this Comma seperated list of models., (*16)
Note :
- except and only options don`t work togther you need to specifiy one of them.
- except and only model names are (class base name not the full name space), (*17)
5.If you run the command it will generate the repositories and interfaces in app\Repositories and RepositoriesServiceProvider in the app\Provider and the terminal will output the names of generated files., (*18)
6-you need to register the RepositoriesServiceProvider .. Go to config/app.php and add this line to the end of the providers array(make sure you change App if you have different namespace for your application)., (*19)
App\Providers\RepositoriesServiceProvider::class
, (*20)
........, (*21)
Now you are good to go , you can go to any class that has automatic resolution (controller , event , command ..) and type-hint the repository interface , and you will get a repository that has an instance of it`s model., (*22)
Happy coding .., (*23)