Download the PHP package hemp/presenter without Composer

On this page you can find all versions of the php package hemp/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

🌿 Hemp Presenter

This package makes it fast, fun, and profitable to decorate your Eloquent models for presentation in views, PDFs, CSV files, or anywhere else in your project.

For a little primer on the problems presenters solve, take a look at this article: Presenters in Laravel.

Installation

Install the package via Composer:

In Laravel 5.5+, the package's service provider should be auto-discovered, so you won't need to register it. If for some reason you need to register it manually you can do so by adding it to the providers array in config/app.php:

Creating Presenter Classes

You can easily generate a Presenter class by calling the make:presenter Artisan command:

This will generate an empty Presenter class inside of app/Presenters.

Customizing Presenter Classes

At their core, presenters are simple classes designed to encapsulate complex or repetitive view logic. What makes hemp/presenter nice is it allows you to attach magic accessors to these Presenter objects all the while allowing for the typical serialization workflow of using regular Model objects and collections. For example, take this ApiPresenter class:

This class has a custom method createdDate that can be called wherever this Presenter is used. It also has a magic accessor getFullNameAttribute that will be accessible via the Presenter like so: $user->full_name. This works exactly like Eloquent's magic accessors...when the Presenter is serialized into a response (like for a view or API response), these magic accessors will be called and added to the rendered output.

You'll notice we're calling $this->first_name and $this->last_name. These are not available on the Presenter class itself, but are being delegated to the underlying Model instance.

This Presenter might output something like this:

Once you have a presented model instance (like inside a Blade view), you can use magic accessors like this:

Or use the methods available on the Presenter itself:

When outputting the Presenter to an array or JSON, if you'd like each of the rendered attributes to use camelCase formatting instead of the default snake_case formatting, you can set the snakeCase property on your Presenter to false:

This will cause the rendered output to look like this:

You might like this option if your front-end JavaScript style guide uses mostly camelCased variables.

In addition, you can set the strategy used at runtime using the snakeCase and camelCase setters:

Presenting Single Models

There are a number of different ways you can present your Model objects, depending on your personal preferences. For instance, you can use the make factory method of the Presenter class:

You can also call the make method on any of your custom Presenter classes, without passing the second argument:

You may also use the present global function, if that's your jam:

Or you can use the Hemp\Presenter\Presentable trait on your Model. This will allow you to call present on it directly:

Also, when using the Presentable trait, you can specify a default presenter using the defaultPresenter attribute on the Model and then calling present:

Presenting Collections

You can also create a collection of presented Model objects. One way is to use the static collection method on the Presenter class to present an array of Model objects:

You can also use the static collection method on any of your custom Presenter classes directly without passing the second argument:

You may also use the present macro on a Collection object:

Hiding Model Attributes From Output

There are times you may wish to keep certain keys from being rendered inside your Presenter. You can use the hidden property on the Presenter to keep any default Model attributes from being used in the output:

This will keep the underlying Model instance's stripe_private_key attribute from showing in the final output.

You may also specify the visible property on the Presenter to act as a whitelist of attributes that should be shown in the output.

Note: If a key is specified in both the hidden and visible properties, then it will be assumed that you want it to be visible in the rendered output.

Support

If you're using this package, I would love to know about it!

If you're having trouble getting something to work when using this package, contact me on Twitter. I'd be glad to help.

If you believe you have found an bug, improvement, or other issue, please report it using the GitHub issue tracker, or fork the repository and submit a pull request.


All versions of presenter with dependencies

PHP Build Version
Package Version
No informations.
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 hemp/presenter contains the following files

Loading the files please wait ....