Download the PHP package andersonef/repositories-pattern without Composer
On this page you can find all versions of the php package andersonef/repositories-pattern. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package repositories-pattern
Repositories Pattern on Laravel 5.1 or superior
This package allows you to implement the Repositories design pattern in your laravel application. It makes easy to use a service layer too.
Instalation
Its simply to install, just run:
composer require andersonef/repositories-pattern
Service Provider
You must register this package service provider at you config/app.php file. Just add this line at your $providers array:
Creating your Repositories and Services:
At your console, enter the following command:
php artisan make:repository BlogNamespace/Post --entity=App/Models/Post
Remember to use the complete namespace to your entity path. If your are using a custom namespace, use:
php artisan make:repository BlogNamespace/Post --entity=CustomNamespace/Models/Post
This will create the following file structure in your app directory:
app/
Repositories/
BlogNamespace/
PostRepository.php
Services/
BlogNamespace/
PostService.php
Repository file structure
Your repository file will be created with the following code:
Service file structure
And your PostService.php file wil be created with the following code:
Usage
Using this pattern you wil be able to separate your application rules from your data access rules and you will be able to reuse your code in a very simple way. Imagine that you have an app that users can register using the public pages AND the admin register users using the admin panel. The rules to register an user are the same on both cases, but the admin panel must require an admin logged user AND has an option to isent the new user to pay his subscription value. In the old way you would have to replicate the code or use a laravel command to isolate the user registration rules. Using the service layer and the repository pattern you will write the user registration rules inside the UserService and on both controllers (public page registration and admin panel) you would call the $userService->create($request->all()). If you must implement an api to save users from an android interface, you can reuse your service layer and just change the way your controller respond to the client.
Repository Inherited Methods:
Your repository have some inherited methods from RepositoryAbstract class. They are:
- create(array $data);: Tries to create a new instance of your specified entity. WARNING: Your entity must declare the $fillable field
- update(array $data, $id);: Update specified entity.
- delete($id);: Delete the specified entity
- *find($id, array $columns = ['']);**: Find an instance of specified entity by id
-
*findBy(array $fields, array $columns = ['']);**: Find a collection of specified instances using the fields (see phpdocs).
Service Inherited Methods:
Your service will be similar to your repository. The main difference is that your service layer must implement application logic, so it have transaction in its methods:
- create(array $data): Open a transaction and try to create an instance using the $data array. You can override it
- update(array $data, $id): Open a transaction and try to update the instance using $data array.
- delete($id): OPen a transaction and try to delete an instance of specified entity.
Magic Methods:
For convenience, we can use magic methods on both service and repository classes:
Using Criterias:
You can implement criteria to reuse your application query rules. This package brings you one default criteria, the FindUsingLikeCriteria. Lets think you must implement a search field on your blog, and must bring all your posts that have some text like $query variable. You can simply do:
This will returns to you a collection of posts that has title, or content, or author like the text inside 'textQuery' request attribute. You can create your own criterias, its really simple to do it:
Creating your own criteria
This wil be your custom criteria:
Credits
This package has been created based on this other: https://github.com/prettus/l5-repository I just implement a few more options and make some fews changes to turn it better for the company I work.