Download the PHP package culturegr/presenter without Composer

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

Latest Version on Packagist Total Downloads Github Actions

This package provides an easy way to create Presenters that can be used to decorate an Eloquent Model. It is heavily inspired by Hemp Presenter and Laravel API Resources, however it comes with some key differences:

TL;DR

A Decorator is an object that wraps another object in order to add functionality to the wrapped object. It also delegates calls to methods not available on the decorator to the class being decorated. Decorators can be useful when you need to modify the functionality of a class without relying on inheritance. You can use it to add optional features to your objects like logging, access-control, and things of that sort. A Presenter is a type of decorator used to present an object to the view of an application, such as a Blade view, or an API response.

Installation

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:

Usage

Creating Presenter Classes

A Presenter class can be generated by running the make:presenter Artisan command:

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

Customizing Presenter Classes

Presenters are simple classes designed to encapsulate complex or repetitive view logic. For example, here is a simple UserPresenter class:

Note that the original model's properties (firstname, lastname) can be directly accessed using the $this variable. This is because the Presenter class automatically proxies property and method access down to the underlying model.

This class has a custom method fullname that can be called wherever an instance of this Presenter is used.

The Presenter class requires you to implement an abstract toArray method which returns the array of attributes that should be used when the presenter is converted to an array or JSON. This ensures consistent behavior across all presenter classes.

Presenting Single Models

Once the presenter is defined, it may be used to create a presented model using the make factory method:

The presented model may be used anywhere in the project and can be passed to views, returned from controllers, or returned from API routes. Presenters implement Arrayable, Jsonable, JsonSerializable, and ArrayAccess interfaces, which means they can be:

Presenting Collections

To present a collection of models, the static collection method on the Presenter class can be used:

The collection of presented models may be used anywhere in the project. Thanks to the automatic conversion features, the collection can be directly returned from a route or controller without explicitly calling toArray():

The output will be something like this:

Presenting Paginated Collections

To present a paginated collection of models with pagination metadata, the static pagination method on the Presenter class can be used:

The paginated collection of presented models may be used anywhere in the project and can be directly returned from a route or controller:

The output will contain the presented models wrapped in a data key, along with links and meta keys containing pagination information:

Using the Presentable Trait

The package provides a Presentable trait that can be used to present models directly from the model instance. This allows for a more elegant syntax when presenting models, collections, and paginators.

To use the trait, first add it to your model:

Then you can present the model directly using the present method:

This allows for a more fluent syntax when presenting models:

For collections, you can use the presentCollection method directly on the collection:

This is equivalent to using the static collection method on the Presenter class, but provides a more fluent API.

You can also use the presentCollection method directly on a paginator instance:

When used on a paginator, the presentCollection method automatically detects that it's being called on a paginator and returns a structured result with the presented models wrapped in a data key, along with links and meta keys containing pagination information.

Automatic Array and JSON Conversion

Presenters now implement the necessary interfaces to be automatically converted to arrays or JSON when needed:

This means you no longer need to explicitly call toArray() or toJson() in most contexts:

Array Access Support

Presenters can now be accessed like arrays:

Abstract toArray Method

The toArray() method is now abstract, which means all presenter classes must implement it. This ensures consistent behavior across all presenters.

Note for Existing Users

If you're currently calling toArray() explicitly when using Presenters (e.g., UserPresenter::make($user)->toArray()), please note that these calls will continue to work as before. However, they are no longer necessary in most contexts due to the new automatic conversion feature. You can safely remove these explicit calls if desired, which will make your code cleaner and more concise.

Testing

License

Please see the license file for more information.

Credits


All versions of presenter with dependencies

PHP Build Version
Package Version
Requires illuminate/support Version ^9.0|^10.0|^11.0|^12.0
ext-json Version *
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 culturegr/presenter contains the following files

Loading the files please wait ....