Download the PHP package ghebby/laravel-hfm without Composer
On this page you can find all versions of the php package ghebby/laravel-hfm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ghebby/laravel-hfm
More information about ghebby/laravel-hfm
Files in ghebby/laravel-hfm
Package laravel-hfm
Short Description A laravel packe aimed to reduce verbose and repetiteve code in the application by generatinc it at runtime starting from a simple Array declaration in the Models.
License MIT
Homepage https://github.com/anatolieghebea/laravel-hfm
Informations about the package laravel-hfm
draft
UNSTABLE
A simple Laravel packge to help DRY your code
This packe aims to reduce verbose and repetiteve code in the application by generatinc it at runtime starting from a simple Array declaration in the Model.
This is an opinionated approach, so make sure it suits your project guide lines before using it.
Installation
- This package publishes a config/laravel-hfm.php file. If you already have a file by that name, you must rename or remove it.
-
You can install the package via composer:
-
Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
-
You should publish the config/laravel-hfm.php config file with:
- If you want to customize the views for the UI helper functions, then run
to bublish them in resources/views/vendor/laravel-hfm/
Usage
Defining a Field Map on the Model may have a lot of benifits in terms of code duplication, making it easier and faster to develop trivil parts of the application.
Let's see a short example. Model name: company Model fileds:
- id
- name
- fiscal_code
- phone
- description
- address
- city
- zip
- country
Tipical approach
In a tipical situation in the model Company.php
we will have something like this
On the CompanyController we will have something like
On the view side a tipical situation may be
I hope it's easy to see the problem. If for some reason a constraint is change we will need to fix the code in a few places:
- in controller@store -> validationRules
- in controller@update -> validationRules
- in view.create -> fields
- in view.edit -> fields
Using this package
The package provides:
- a Contract Interface to be implemented by the model
- a HfmTrait that contains the most used methods for manipolating the Field Map
- a HfmConst and HfmHelper which gets autoloaded on app boot, in order to define the cosnstants
- a few view helper functions which have the ability to generate a UI given a FieldMap
Let's how the code changes when sing this package:
Model Company.php
On controller
On the view side
With this approach we centralized the source of truth in the application, if we change the label for the filed name
in Company.php it will propagete to all the views that relies on the _standardForm
helper function.
If in the Field Map on the filed fiscal_code
the attribute required
is set to FALSE, the change will immediately affect the store and update methods in Controller and also the required attribute on the input field.
Adding removing fields
Adding or removing a field to an Model entity, is just the mater of adding o removing a line in the getFieldMap()
of the model.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Anatolie Ghebea
- All Contributors
License
The MIT License (MIT). Please see License File for more information.