Giter Site home page Giter Site logo

rhynodesigns / laravel-presenter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from touhidurabir/laravel-presenter

0.0 0.0 0.0 21 KB

A package that helps to group methods that mostly use for the view presentation purpose form models to a dedicated presenter class.

License: MIT License

PHP 100.00%

laravel-presenter's Introduction

Laravel Presenter

A package that helps to group methods that mostly use for the view presentation purpose.

Installation

Require the package using composer:

composer require touhidurabir/laravel-presenter

To publish the config file:

php artisan vendor:publish --provider="Touhidurabir\Presenter\PresenterServiceProvider" --tag=config

Configurations

Check the published config file for the details of configurations options.

Usage

First step is to generate a presenter class . To make this process easy, this package comes with a command that helps to generate the presenter classes. To generate the presenter class via command, do as

php artisan make:presenter PresenterClassName

For example :

php artisan make:presenter UserPresenter

will generate a UserPresenter at the App\Presenters namespaced path.

it will have following content

namespace Touhidurabir\Presenter\Tests\App\Presenters;

use Touhidurabir\Presenter\BasePresenter;

class UserPresenter extends BasePresenter {

}

Now to use this with the combination of User model class, drop the HasPresenter trait and defined property $presenter as :

use Illuminate\Database\Eloquent\Model;
use Touhidurabir\Presenter\HasPresenter;
use Touhidurabir\Presenter\Tests\App\Presenters\UserPresenter;

class User extends Model {

    use HasPresenter;

    protected $presenter = UserPresenter::class;
}

And Now any public method defined in the UserPresenter class can be accesses as :

$user->present()->someMethod

It is also possible to not define the $presenter property while using the presenter class for models . In that case it will try to resolve the presenter through the model name and defaule store namespace defined in the config file .

For example, by default it will try to find a class in the App\Presenters location with name of UserPresenter for User model if the $presenter property not set in the User model. If it can find it, then it will use that for User model presenter . If none found, it will throw exception.

It is also possible to set/update the presenter on fly . To do that , need to user the setPresenter method defined in the HasPresenter trait as :

$user->setPresenter(UserPresenter::class);
$user->present()->anyMethodDefinedInPresenter;

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

laravel-presenter's People

Contributors

touhidurabir avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.