Introduction
Laravel Presenter by @ousid is a clean way to decorate your models, and incrementally refactor from the simplest solution to a clean, reusable system.
Prerequisite
You need to know the following:
- Need to know PHP
- Basics on Laravel & the artisan command Laravel
- Basics on composer [Composer][https://getcomposer.org]
Why this package?
Sometimes we need to present some custom attributes and re-use them in many places in our app, and the simplest way is to put them inside your model
class, but is it efficient?
This page allows you to make a place for all your custom attribute methods and decorate them to use them wherever you want, and help you to maintain/refactor your code easily.
Installation
You can install the package via composer:
1composer require coderflexx/laravel-presenter
You can publish the config file with:
1vendor:publish --provider="Coderflex\LaravelPresenter\LaravelPresenterServiceProvider"
This is the contents of the published config file:
1return [ 2 3 /* 4 |-------------------------------------------------------------------------- 5 | Presenter Namespace 6 |-------------------------------------------------------------------------- 7 | 8 | This value informs LaravelPresenter which namespace you will be 9 | selecting to store your presenters by default.10 | If this value equals to null, "App\Presenter" will be used11 | by default.12 |13 */14 15 'presenter_namespace' => 'App\\Presenters',16];
Usage
The implementation of this package is so simple, all what you need to do is the following:
Model Implementation
- Implement
CanPresent
Interface - Use
UsesPresenters
Trait
1use Coderflex\LaravelPresenter\Concerns\CanPresent; 2use Coderflex\LaravelPresenter\Concerns\UsesPresenters; 3// ... 4 5class User extends Authenticatable implements CanPresent 6{ 7 use UsesPresenters; 8 9 // ...10}
Create New Model Presenter class
This package gives you an easy way to generate new Presenter
class, all you need to do is to use presenter:make
command.
1php artisan presenter:make UserPresneter
UserPresenter
in our case, leaves by default in App\Presenters
.
This is the contents of the UserPresenter
file:
1<?php 2 3namespace App\Presenters; 4 5use Coderflex\LaravelPresenter\Presenter; 6 7class UserPresenter extends Presenter 8{ 9 //10}
If you want to change the directory, you have two options.
First options is to set the full namespace while you're creating the presenter class
1php artisna presneter:make App\Models\Presenter\UserPresenter
Or change presenter_namespace
from config/laravel-presenter
file.
1return [2 ...3 4 'presenter_namespace' => 'App\\Presenters',5 6 ...7];
Using the Presenter
Generated Class
After you create the presenter class, you need to register it on the Model
by adding the $presenters
protected property:
1use App\Presenters\UserPresenter; 2use Coderflex\LaravelPresenter\Concerns\CanPresent; 3use Coderflex\LaravelPresenter\Concerns\UsesPresenters; 4// ... 5 6class User extends Authenticatable implements CanPresent 7{ 8 use UsesPresenters; 9 10 protected $presenters = [11 'default' => UserPresenter,12 ];13}
By default, the type of your presenter class is default
, but you can use as many of presenters you want, just by identifying the type in $presenters
property.
Real Life Example
Now, after we generated the presenter
class, and we implemented it successfully in our model, we can use it like so:
In your UserPresenter
class or any presenter class you generated.
1...2class UserPresenter extends Presenter3{4 public function fullName()5 {6 return "{$this->model->first_name} {$this->model->last_name}";7 }8}9...
We add a new method to present the fullName
.
In your blade or any place you want, you can do:
1$user->present()->fullName
Your application will show the full name from the method you added.
Adding Another Presenter Type:
Like I said above, by default the type will be deafult
but, you can add more types as you need.
Here is an example:
1use App\Presenters\UserPresenter; 2use Coderflex\LaravelPresenter\Concerns\CanPresent; 3use Coderflex\LaravelPresenter\Concerns\UsesPresenters; 4// ... 5 6class User extends Authenticatable implements CanPresent 7{ 8 use UsesPresenters; 9 10 protected $presenters = [11 'default' => UserPresenter,12 'setting' => UserSettingPresenter,13 ];14}
Generate new UserSettingPresenter
1php artisan presenter:make UserSettingPresenter
Add anything to UserSettingPresenter
method
1...2class UserSettingPresenter extends Presenter3{4 public function lang()5 {6 return $this->model->settings->defaultLang;7 }8}9...
Finally, set setting
as a type:
1$user->present('setting')->lang;
By that, you can split your logic and make your code base even cleaner.
Changelog
You can follow the changes on this package by accessing the changelog or the releases
Contributing
If you want to contribute, feel free to read the CONTRIBUTING guidelines, and make your PR
Conclusion
Today we s learned about Laravel Presenter package, and how to use it. To make our development process easier and save us time.