Download the PHP package sourcetoad/enhanced-resources without Composer

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

Enhanced Resources

Enhancements for Laravel's API resources.

Installation

Basic Usage

To create an enhanced resource you simply extend Sourcetoad\EnhancedResources\Resource instead of Illuminate\Http\Resources\Json\JsonResource and provide a format method.

Formatting

With EnhancedResources you can have multiple formats for a single resource by adding format methods. Format methods are defined using the #[Format] attribute.

If only a single format method is defined, as is the case in the example above in the basic usage section, that format will be the default format that is used when resolving the resource. However, you can define as many formats as you like.

In cases like the one above you'll need to specify the format to be used by providing its name to the format() method. By default the format uses the same name as the method, so in this example we have format names of bar, foo, and foobar.

Failing to specify the format in a situation where there is no default format will result in a NoFormatSelectedException being thrown.

Specifying a Default

If you don't want to always explicitly specify the format to be used when you have a resource with multiple formats you can specify one format as default using the #[IsDefault] attribute.

After adding the #[IsDefault] attribute to one of your format methods it will be used unless the format is explicitly specified via the format() method.

Specifying more than one default method via the #[IsDefault] attribute will result in a MultipleDefaultFormatsException being thrown.

The #[IsDefault] attribute is detected on a per-class basis up the inheritance chain, so you can define a format as #[IsDefault] on a parent resource and override it with another #[IsDefault] format on the child resource without triggering a MultipleDefaultFormatsException. However, if no #[IsDefault] format is defined on the child resource the one on the parent will still be used.

Naming Formats

You can also override the name of formats and even provide multiple names for a single format. Let's look at the following example:

In this example we have three formats, but six names:

The primary name of each format is the first instance of the #[Format] attribute, and the rest are aliases. This means that the primary names would be: bar, foo, and c in the example above. In most cases this distinction should not come into play.

Collections

Both anonymous collections and defined resource collections utilize the formats of the underlying resource objects, and follow all the same rules.

Modifications

Modifications allow you to tweak the output of resources on the fly. They are applied similarly to how state is applied for Eloquent factories. The most basic form of modification is a simple array merge modification done by providing an array to the modify method of a resource:

To accomplish more complex modifications you can also pass any callable that accepts (array $data, Resource $resource). It is important when using these types of modifications to return the data as failing to do so will result in resource's data being replaced with null.

You can also define methods on the resource class itself that can make modifications via calling the modify method.

Except

The except enhancement is a modification class and trait combination that allows for the easy exclusion of certain fields from a resource.

Only

The only enhancement is a modification class and trait combination that allows for the easy exclusion of certain fields from a resource.

Additional Enhancements

EnhancedResources also includes a couple of other helpful enhancements.

Status Codes

You can now tweak the status code of the resource response with a simple call to the setResponseStatus() method.

ConvertsToResource

You can provide any object with a toResource method with a simple trait and attribute combination:

Testing

Testing endpoints that respond with enhanced resources is recommended to be done using Laravel's existing response assertions. One approach to creating resource asserter objects to help simplify the process that leverages functionality provided by enhanced resources can be found here.


All versions of enhanced-resources with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
laravel/framework Version ^10.0|^11.0
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 sourcetoad/enhanced-resources contains the following files

Loading the files please wait ....