|
1 | 1 | # Laravel Presenter
|
2 | 2 |
|
3 |
| -[](https://packagist.org/packages/coderflex/laravel-presenter) |
| 3 | +[](https://packagist.org/packages/coderflex/laravel-presenter) |
4 | 4 | [](https://github.com/coderflexx/laravel-presenter/actions?query=workflow%3Arun-tests+branch%3Amain)
|
5 | 5 | [](https://github.com/coderflexx/laravel-presenter/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
|
6 | 6 |
|
7 |
| -A clean way to present your model attributes without putting them in the worng file. |
| 7 | +A clean way to present your model attributes without putting them in the wrong file. |
8 | 8 |
|
9 | 9 | ## Installation
|
10 | 10 |
|
@@ -42,9 +42,168 @@ return [
|
42 | 42 | ```
|
43 | 43 |
|
44 | 44 | ## Usage
|
| 45 | +The implementation of this package is so simple, all what you need to do is the following: |
45 | 46 |
|
46 |
| -<!-- TODO: document the package usage --> |
| 47 | +## Model Implementation |
47 | 48 |
|
| 49 | +- Implement `CanPresent` Interface |
| 50 | +- Use `UsesPresenters` Trait |
| 51 | + |
| 52 | +```php |
| 53 | +use Coderflex\LaravelPresenter\Concerns\CanPresent; |
| 54 | +use Coderflex\LaravelPresenter\Concerns\UsesPresenters; |
| 55 | +// ... |
| 56 | + |
| 57 | +class User extends Authenticatable implements CanPresent |
| 58 | +{ |
| 59 | + use UsesPresenters; |
| 60 | + |
| 61 | + // ... |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Create New Model Presenter class |
| 66 | + |
| 67 | +This package gives you an easy way to generate new `Presenter` class, all you need to do is to use `presenter:make` command. |
| 68 | + |
| 69 | +```bash |
| 70 | +php artisan presenter:make UserPresneter |
| 71 | +``` |
| 72 | + |
| 73 | +`UserPresenter` in our case, leaves by default in `App\Presenters`. |
| 74 | + |
| 75 | +This is the contents of the `UserPresenter` file: |
| 76 | + |
| 77 | +```php |
| 78 | +<?php |
| 79 | + |
| 80 | +namespace App\Presenters; |
| 81 | + |
| 82 | +use Coderflex\LaravelPresenter\Presenter; |
| 83 | + |
| 84 | +class UserPresenter extends Presenter |
| 85 | +{ |
| 86 | + // |
| 87 | +} |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +If you want to change the directory, you have two options. |
| 93 | + |
| 94 | +First options is to set the full namespace while you're creating the presenter class |
| 95 | + |
| 96 | +```bash |
| 97 | +php artisna presneter:make App\Models\Presenter\UserPresenter |
| 98 | +``` |
| 99 | + |
| 100 | +Or change `presenter_namespace` from `config/laravel-presenter` file. |
| 101 | + |
| 102 | +```php |
| 103 | +return [ |
| 104 | + ... |
| 105 | + |
| 106 | + 'presenter_namespace' => 'App\\Presenters', |
| 107 | + |
| 108 | + ... |
| 109 | +]; |
| 110 | +``` |
| 111 | + |
| 112 | +## Using the `Presenter` Generated Class |
| 113 | +After you create the presenter class, you need to register it on the `Model` by adding the `$presenters` protected property: |
| 114 | + |
| 115 | +```php |
| 116 | +use App\Presenters\UserPresenter; |
| 117 | +use Coderflex\LaravelPresenter\Concerns\CanPresent; |
| 118 | +use Coderflex\LaravelPresenter\Concerns\UsesPresenters; |
| 119 | +// ... |
| 120 | + |
| 121 | +class User extends Authenticatable implements CanPresent |
| 122 | +{ |
| 123 | + use UsesPresenters; |
| 124 | + |
| 125 | + protected $presenters = [ |
| 126 | + 'default' => UserPresenter, |
| 127 | + ]; |
| 128 | +} |
| 129 | +``` |
| 130 | +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. |
| 131 | + |
| 132 | +## Example |
| 133 | +Now, after we generated the `presenter` class, and we implemented it successfully in our model, we can use it as so: |
| 134 | + |
| 135 | +In your `UserPresenter` class or any presenter class you generated. |
| 136 | + |
| 137 | +```php |
| 138 | +... |
| 139 | +class UserPresenter extends Presenter |
| 140 | +{ |
| 141 | + public function fullName() |
| 142 | + { |
| 143 | + return "{$this->model->first_name} {$this->model->last_name}"; |
| 144 | + } |
| 145 | +} |
| 146 | +... |
| 147 | +``` |
| 148 | + |
| 149 | +We add a new method to present the `fullName`. |
| 150 | + |
| 151 | +In your blade or any place you want, you can do: |
| 152 | + |
| 153 | +```php |
| 154 | +$user->present()->fullName |
| 155 | +``` |
| 156 | +Your application will show the full name from the method you added. |
| 157 | + |
| 158 | +## Adding Another Presenter Type: |
| 159 | +Like I said above, by default the type will be `deafult` but, you can add more types as you need. |
| 160 | + |
| 161 | +Here is an example: |
| 162 | + |
| 163 | +```php |
| 164 | +use App\Presenters\UserPresenter; |
| 165 | +use Coderflex\LaravelPresenter\Concerns\CanPresent; |
| 166 | +use Coderflex\LaravelPresenter\Concerns\UsesPresenters; |
| 167 | +// ... |
| 168 | + |
| 169 | +class User extends Authenticatable implements CanPresent |
| 170 | +{ |
| 171 | + use UsesPresenters; |
| 172 | + |
| 173 | + protected $presenters = [ |
| 174 | + 'default' => UserPresenter, |
| 175 | + 'setting' => UserSettingPresenter, |
| 176 | + ]; |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +Generate new `UserSettingPresenter` |
| 181 | + |
| 182 | +```bash |
| 183 | +php artisan presenter:make UserSettingPresenter |
| 184 | +``` |
| 185 | + |
| 186 | +Add anything to `UserSettingPresenter` method |
| 187 | + |
| 188 | +```php |
| 189 | +... |
| 190 | +class UserSettingPresenter extends Presenter |
| 191 | +{ |
| 192 | + public function lang() |
| 193 | + { |
| 194 | + return $this->model->settings->defaultLang; |
| 195 | + } |
| 196 | +} |
| 197 | +... |
| 198 | +``` |
| 199 | + |
| 200 | +Finally, set `setting` as a type: |
| 201 | + |
| 202 | +```php |
| 203 | +$user->present('setting')->lang; |
| 204 | +``` |
| 205 | + |
| 206 | +By that, you can split your logic and make your code base even cleaner. |
48 | 207 |
|
49 | 208 | ## Testing
|
50 | 209 |
|
|
0 commit comments