Download the PHP package soyhuce/laravel-json-resources without Composer

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

An opinionated JsonResource for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

Don't ever run database queries in JsonResource serialization, without overhead in production !

Installation

You can install the package via composer:

You can publish the config file with:

Usage

This package provides a base class Soyhuce\JsonResources\JsonResource.

It gives a simplified interface for data serialization :

It is still possible tu use public function toArray($request): array method.

A base class \Soyhuce\JsonResources\ResourceCollection est aussi disponible.

Forbid to execute database queries

Using this base resource, you can forbid to execute database queries during serialization.

You just need to activate this option in json-resources.forbid-database-queries config.

This configuration is thought to be used in local environments but should be deactivated in production. It allows you to tests that all required relations are correctly loaded (in feature tests for exemple) while limiting overhead in production.

For exemple :

A DatabaseQueryDetected exception will be raised with every executed request :

AnonymousResource

It could be useful to have anonymous resources for some cases.

For exemple if user's role is nullable, you could have :

Via an AnonymousResource, you could do

If the user has a role, you will get :

If the user doest not have a role, you will get :

Returning anonymous resource from the controller

It is possible to return an anonymous resource from the controller. In cas the provided data is null, you won't get ' null' but an empty array [].

If the item is found

If the item is null

Note : Anonymous resource collections are not supported

AnonymousResource without format

It is possible to return an anonymous resource without callback. In this case, the resource will be returned as is.

Le json retourné aura alors la forme

Json encoding

All returned jsons are encoded by default with JSON_PRESERVE_ZERO_FRACTION option.

Feature tests

The JsonResource and ResourceCollection classes will add a X-Json-Resource header to the response when the application is in local or testing environment.

This header will contain the actual resource class used to generate the response and can be used in functional tests to verify which resource was used.

In order to test this, you cas use TestResponse::assertJsonResource(TheResource::class) method.

Unit tests

It can be useful to unit test the JsonResource. You can use soyhuce/laravel-testing for this.

In this cas, you will probably need to allow database queries to be run. You can do this calling

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-json-resources with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
spatie/laravel-package-tools Version ^1.9.2
illuminate/contracts Version ^11.0|^12.0
illuminate/http Version ^11.0|^12.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 soyhuce/laravel-json-resources contains the following files

Loading the files please wait ....