Download the PHP package ryannielson/prez without Composer
On this page you can find all versions of the php package ryannielson/prez. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ryannielson/prez
More information about ryannielson/prez
Files in ryannielson/prez
Package prez
Short Description Easily create presenters for your objects.
License MIT
Homepage https://github.com/RyanNielson/prez
Informations about the package prez
Prez
Simple presenters for your PHP or Laravel project.
Why Should I Use Presenters?
Imagine your application has a User
model. With Prez, you'd create a matching UserPresenter
class. This presenter wraps the model, dealing with only presentational concerns. This keeps any view related logic out of the model, while also helping you keep logic out of the view. In your view you can use the presenter in the same way you'd use the original model. Whenever you start thinking about creating a helper function or adding logic to your view, it might be worth moving it into a presenter.
Installation
Run the following Composer command in your terminal, or simply add "ryannielson/prez": "~1.0.0"
to your composer.json file:
composer require ryannielson/prez:'~1.0.0'
Once complete, if using Laravel, you now have to add the the service provider to the providers array in app/config/app.php
:
'RyanNielson\Prez\PrezServiceProvider'
Usage
Creating Presenters
Presenters inherit from RyanNielson\Prez\Presenter
, and should be named for the model they present. In Laravel these should live in the app/presenters
directory.
Laravel Commands
Prez includes a command to automate the creation of Presenters.
To create a presenter called UserPresenter
in app/presenters
:
php artisan prez:presenter User
To create a presenter called UserPresenter
in app/custom
:
php artisan prez:presenter User --path=app/custom
Accessing the Object in a Presenter
You can access the wrapped object in a presenter by using $this->object
. This allows you get get any public property or call any public function on the object.
You can also use the presents
property on the presenter so that you can using a name other than $this->object
to access the object. This makes the presenter code a bit more clear.
Getting a Presenter for an Object
By default, Prez assumes that your presenter class uses your object's class name followed by presenter. For example, an object with the class User
is assumed to have a corresponding UserPresenter
class. The presenter
helper method uses this assumption to find your presenter and return an instance of it.
presenter
also takes an optional class name if you want to force a specific presenter to be used.
If you prefer not to use the helper function, you can explicitily pass your object when constructing a presenter.
Using a Presenter in a View
If you have a presenter object passed to your view, you can use your presenter like any other object. The following example assumes the usage of Laravel's blade template language.
Delegating to Object
If the presenter doesn't contain a property or method, the call is delegated to the wrapped object. This makes it so that if we want to access a field on the model, we don't have to write a function in the presenter.