Download the PHP package robclancy/presenter without Composer

On this page you can find all versions of the php package robclancy/presenter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package presenter

Presenter

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.

Ping me @robboclancy for any urgent issues, github isn't always correctly notifying me.

This library provides a simple class to help make a Presenter for your objects or arrays. It also has little extras for use within Laravel with minimal extra code in your controllers (in most cases no extra code).

Tests

Table of Contents

Installation

or

Add robclancy/presenter to the "require" section of your composer.json file.

Run composer update to get the latest version of the package.

Laravel

This package comes with an optional service provider for Laravel 5.8+ < Laravel 10 so that you can automate some extra steps. You will need to have installed using the composer method above, then register the service provider with your application.

Open app/config/app.php and find the providers key. Add

to the array at some point after

Now presenters will automatically be created if using the laravel method described below.

Usage

Presenter is a very simple class that overloads methods and variables so that you can add extra logic to your objects or arrays without adding view logic to areas like your models or controllers and also keeps any extra logic out of your views.

General Usage

Let's say you have a list of users and you want to generate a link to the profile of each user. Many people would just build the URL in the view, or worse, in the controller. To separate this logic we instead use a presenter. Let's assume we have a User class which simply has an id and username property. The presenter might look like this.

Now our view should receive an instance of this presenter which would be created with something like $user = new UserPresenter(new User);. If we want to link to the users page all we have to do is call $user->url(). Now you have good separation of logic and an easy little class you can modify to add properties to your User in all areas. However, you might not want to be calling methods like this, it could be inconsistent with what you are doing, or you might want the code to look a little cleaner. That is where methods with the present prefix come in. All we do is update the presenter to the following.

Now the presenter will call this new method when you execute $user->url. Further more you can access this method via ArrayAccess by calling $user['url']. More on ArrayAccess support below.

Manually Initiate

As mentioned in the above section to create a presenter you simply initiate with the new keyword and inject your object or array.

Laravel Usage

If you are using laravel and have followed the above installation instructions you can use the provided interface Robbo\Presenter\PresentableInterface to automate turning your model instances into a Presenter from both collections and when a model is sent directly to the view.

What the service provider does is extend Laravel's view component with a step before the view object is created. This step turns anything that implements the PresentableInterface into a Presenter by calling ->getPresenter(). What this means is you don't need to add anything extra to your controllers to have your views using presenters for your objects.

For Example.

Now whenever your User model is sent to a view, in a collection, array or by itself it will be turned into a presenter using the provided getPresenter() method. So your controller will work with User and when you get to your view it will be working with UserPresenter with the internal object being User.

Array Usage

1.1.x introduces support for arrays. The Presenter will implement ArrayAccess so in your views you can access your variables with $presenter['variable'] if you want. But more importantly you can give the Presenter an array instead of an object. So you can use presenters to work with array data as well as objects.

For example.

Extending the Decorator

As of 1.2.x I have added in a decorator object. This object takes care of turning an object that has PresentableInterface into a Presenter. By default, this is done with Laravel's View objects. The reasoning behind a new class instead of the previous implementation is so it can be better tested and also to allow you to extend it. Here is an example of extending the Decorator so that instead of using the PresentableInterface and getPresenter() method you can use a public variable on the object called $presenter.

Note: these instructions are for Laravel usage.

First extend the decorator...

To use your new decorator either add the following to start/global.php or into your own service provider.

And that is all there is to it. You can easily automate the creation of presenters to suit your workflow using this method.

Change Log

3.0

2.0.1

2.0.0

1.4.0

1.3.3

1.3.2

1.3.1

1.3.0

1.2.0

1.1.0

1.0.2

1.0.1

1.0.0


All versions of presenter with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package robclancy/presenter contains the following files

Loading the files please wait ....