willishq / laravel-components
Components for Laravel 5
Requires
- php: >=5.4.0
- barryvdh/laravel-debugbar: 1.*
- illuminate/html: ~5.0
- illuminate/support: 5.0.*
- laracasts/commander: ~1.0
- laracasts/flash: ~1.0
- laracasts/presenter: ~0.2.0
- league/fractal: 0.8.*
Requires (Dev)
- phpunit/phpunit: 4.2.*
This package is auto-updated.
Last update: 2022-02-01 12:40:01 UTC
README
These are components which I use frequently when creating and maintaining a Laravel project, and two traits which I keep copying from project to project.
Installation
Grab this package through Composer:
{ "require" { "willishq/laravel-components": "1.*" } }
Traits
The two traits provided are the EventableTrait
and the RetrievableTrait
.
EventableTrait Usage
The EventableTrait
is used in tandem with laracasts\commander
package and enables you to raise and dispatch events
directly on your objects (such as Eloquent models), along with firing events when the object is destroyed.
MyModel.php
use Willishq\LaravelComponents\EventableTrait; use Illuminate\Database\Eloquent\Model; class MyModel extends Model { use EventableTrait; /** * Whether to dispatch remaining events when the object is tore down * * @var bool */ protected $dispatchOnDestruct = true; }
$model = new MyModel; $model->raise(new ModelWasInstantiatedEvent($model));
if you left the model like this, the event would be fired automatically by the __destruct
method, or you can call
$model->dispatchEvents();
which would dispatch all remaining events.
RetrievableTrait Usage
The RetrievableTrait
enables you to retrieve specific public
keys from your objects. It provides you with two methods,
retrieveOnly
and retrieveExcept
use Willishq\LaravelComponents\RetrievableTrait; class Book { use RetrievableTrait; public $title; public $author; public $publish_date; public $blurb; public function __construct($title, $author, $publish_date, $blurb) { $this->title = $title; $this->author = $author; $this->publish_date = $publish_date; $this->blurb = $blurb; } }
$book = new Book('My Amazing Book', 'Andrew Willis', '19/04/2020', 'Andrew was a normal person from Sunderland. you\'ll never believe what happened next.'); $fields = $book->retrieveOnly('title', 'author'); // ['title' => 'My Amazing Book', 'author' => 'Andrew Willis'] $fields = $book->retrieveExcept('blurb'); // ['title' => 'My Amazing Book', 'author' => 'Andrew Willis', '19/04/2020']
I have found this useful when dealing with DTO's.
Packages
Please be sure to add the following to your config/app.php
file to use the packages!
Service Providers:
// Laravel DebugBar 'Barryvdh\Debugbar\ServiceProvider', // Laracasts Commander package 'Laracasts\Commander\CommanderServiceProvider', // Laracasts Flash package 'Laracasts\Flash\FlashServiceProvider', // Laravel 5 requires you to include the HTML package yourself 'Illuminate\Html\HtmlServiceProvider',
Facades:
'Debugbar' => 'Barryvdh\Debugbar\Facade', 'Form' => 'Illuminate\Html\FormFacade', 'Flash' => 'Laracasts\Flash\Flash',
Other packages included are: laracasts/presenter
and league/fractal
.
Documentation for all of the packages included can be found here:
- fractal.thephpleague.com
- github.com/laracasts/Presenter
- github.com/laracasts/flash
- github.com/laracasts/commander
- github.com/barryvdh/laravel-debugbar
For development and testing, I suggest you use codeception/codeception
, laracasts/testdummy
and fzaninotto/faker