Download the PHP package rougin/dexter without Composer
On this page you can find all versions of the php package rougin/dexter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rougin/dexter
More information about rougin/dexter
Files in rougin/dexter
Package dexter
Short Description "Ready-to-eat" CRUD for PHP backend.
License MIT
Homepage https://roug.in/dexter/
Informations about the package dexter
Dexter
Dexter is a utility PHP package that provides extensible PHP classes for handling CRUD operations. It can also create HTTP routes that conforms to the PSR-07 standard.
Installation
Install the Dexter package via Composer:
Using Depot
The Depot class is a special PHP class which provides methods related to CRUD operations (e.g., create, delete, find, update):
Using the Depot class improves development productivity as it reduces writing of code relating to CRUD operations. As it is also designed to be extensible, it can be used freely without the required methods.
[!NOTE] In other PHP frameworks and other guides,
Depotis also known asRepositoryfrom the Repository pattern.
If a Depot class is used, the following methods must be defined depending on its usage:
create method
The create method will be used for creating an item based on the provided payload:
If the specified method is being called, its logic must be defined from the Depot class:
If the required logic for the create method is not defined, it will throw a LogicError.
delete method
When deleting specified items, the delete method can be used from the Depot class:
Using the delete method also requires other methods deleteRow and rowExists to be defined:
If the required logic for the delete method is not defined, a LogicError will be thrown.
find method
The find method is one of the CRUD operations that tries to find an item based on the given unique identifier (e.g., id):
To use the find method, kindly write its logic in the findRow method:
If the specified identifier does not exists, it must throw an UnexpectedValueException. Likewise, if the required logic for the find method is not defined, it will throw a LogicError.
get method
One of the methods of Depot that returns an array of items based on the specified page number and its rows to be shown per page:
To use the get method, the methods getItems and getTotal must be defined:
If the logic requires an offset instead of a page number, the getOffset method from Depot can be used to compute the said offset value:
Using the get method returns a Result class, which can be used for handling the result from the Depot:
Each item from the Result class can also be parsed manually using the asRow class:
If the required logic for the get method is not defined, a LogicError will be thrown.
update method
The update method is used to update details of the specified item:
When using the update method, its required logic must also be defined:
If the logic for the update method is not defined, it will throw a LogicError.
Using EloquentDepot
The EloquentDepot class provides a pre-built implementation of Depot that delegates all CRUD operations to an Eloquent Model:
Once defined, all CRUD methods are available out of the box:
[!NOTE] The
EloquentDepotclass requires theilluminate/databasepackage, which should be installed as a dependency:
Filtering results
The get method supports filtering through the withFilter method. Use the Filter class to add WHERE clauses to the paginated query:
The specified filters can be used as as* method in getItems:
To identify filter fields as searchable (using LIKE), use the withSearch method:
The searchable fields can be used in the depot with getSearchKeys method:
Using Route
The Route class in Dexter is similar to the previously discussed Depot class. While the Depot class conforms to the CRUD operations, the Route class closely follows the RESTful software architecture style and uses the PSR-07 standard for standardization of its HTTP responses:
The Route class contains the following methods for writing their logic:
is[METHOD]Valid
This method will be triggered if [METHOD] requires to be validated first. If not specified, it always return to true by default:
invalid[METHOD]
This method will be triggered if the is[METHOD]Valid method returns to false. This should return an HTTP response with an HTTP code between 4xx to 5xx:
set[METHOD]Data
This is the main method that requires to write its logic based on [METHOD]:
Using this kind of approach improves the code structure of HTTP routes as it only requires to write the logic for each Route method that is being used (e.g., index).
[!NOTE] In other PHP frameworks and other guides,
Routeis also known asController.
delete method
The delete method is an HTTP route which can be used for deleting a specified item:
index method
The index method should return an array of items as its HTTP response:
show method
The show method returns an HTTP response for the specified item:
store method
The store method should be responsible for creating new items to the specified storage:
update method
The update method updates the details of a specified item:
Unified validation
When multiple actions share the same validation logic, use the invalid, isAllowed, and isDataValid methods to avoid duplication.
invalid
Returns an error response for any action. The $code parameter defaults to 400:
All per-action methods (e.g., invalidDelete, invalidStore, etc.) are delegated to this method by default. Override the invalid method once when the error response is the same for all actions.
isAllowed
Checks whether the action is allowed (e.g., checking if the user is authorized):
All action methods delegated to this method by default. Override this method if all actions must have the same authorization logic.
isDataValid
Checks whether the payload data is valid for the store and update actions:
The isStoreValid and isUpdateValid methods are only delegated to methods isAllowed and isDataValid.
Changelog
Please see CHANGELOG for more recent changes.
Contributing
See CONTRIBUTING on how to contribute.
License
The MIT License (MIT). Please see LICENSE for more information.