Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Decorate Your Models and Write Clean/Reusable Code with Presenters.

License

NotificationsYou must be signed in to change notification settings

coderflexx/laravel-presenter

Repository files navigation

The Latest Version on PackagistGitHub Tests Action StatusGitHub Code Style Action StatusTotal Downloads

A clean way to present your model attributes without putting them in the wrong file.

Installation

You can install the package via composer:

composer require coderflexx/laravel-presenter

You can publish the config file with:

php artisan vendor:publish --provider="Coderflex\LaravelPresenter\LaravelPresenterServiceProvider"

This is the contents of the published config file:

return [/*    |--------------------------------------------------------------------------    | Presenter Namespace    |--------------------------------------------------------------------------    |    | This value informs LaravelPresenter which namespace you will be    | selecting to store your presenters by default.    | If this value equals to null, "App\Presenter" will be used    | by default.    |    */'presenter_namespace'   =>'App\\Presenters',];

Usage

The implementation of this package is so simple, all what you need to do is the following:

Model Implementation

  • ImplementCanPresent Interface
  • UseUsesPresenters Trait
useCoderflex\LaravelPresenter\Concerns\CanPresent;useCoderflex\LaravelPresenter\Concerns\UsesPresenters;// ...class Userextends Authenticatableimplements CanPresent{use UsesPresenters;// ...}

Create New Model Presenter class

This package gives you an easy way to generate newPresenter class, all you need to do is to usepresenter:make command.

php artisan presenter:make UserPresenter

UserPresenter in our case, leaves by default inApp\Presenters.

This is the contents of theUserPresenter file:

<?phpnamespaceApp\Presenters;useCoderflex\LaravelPresenter\Presenter;class UserPresenterextends Presenter{//}

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

php artisna presenter:make App\Models\Presenter\UserPresenter

Or changepresenter_namespace fromconfig/laravel-presenter file.

return [    ...'presenter_namespace'   =>'App\\Presenters',    ...];

Using thePresenter Generated Class

After you create the presenter class, you need to register it on theModel by adding the$presenters protected property:

useApp\Presenters\UserPresenter;useCoderflex\LaravelPresenter\Concerns\CanPresent;useCoderflex\LaravelPresenter\Concerns\UsesPresenters;// ...class Userextends Authenticatableimplements CanPresent{use UsesPresenters;protected$presenters = ['default' => UserPresenter,    ];}

By default, the type of your presenter class isdefault, but you can use as many of presenters you want, just by identifying the type in$presenters property.

Example

Now, after we generated thepresenter class, and we implemented it successfully in our model, we can use it like so:

In yourUserPresenter class or any presenter class you generated.

...class UserPresenterextends Presenter{publicfunctionfullName()    {return"{$this->model->first_name}{$this->model->last_name}";    }}...

We add a new method to present thefullName.

In your blade or any place you want, you can do:

$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 bedefault but, you can add more types as you need.

Here is an example:

useApp\Presenters\UserPresenter;useCoderflex\LaravelPresenter\Concerns\CanPresent;useCoderflex\LaravelPresenter\Concerns\UsesPresenters;// ...class Userextends Authenticatableimplements CanPresent{use UsesPresenters;protected$presenters = ['default' => UserPresenter,'setting' => UserSettingPresenter,    ];}

Generate newUserSettingPresenter

php artisan presenter:make UserSettingPresenter

Add anything toUserSettingPresenter method

...class UserSettingPresenterextends Presenter{publicfunctionlang()    {return$this->model->settings->defaultLang;    }}...

Finally, setsetting as a type:

$user->present('setting')->lang;

By that, you can split your logic and make your code base even cleaner.

Testing

composertest

Changelog

Please seeCHANGELOG for more information on what has changed recently.

Contributing

Please seeCONTRIBUTING for details.

Security Vulnerabilities

Please reviewour security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please seeLicense File for more information.

About

Decorate Your Models and Write Clean/Reusable Code with Presenters.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

    Contributors6

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp