Download the PHP package f9webltd/laravel-api-response-helpers without Composer

On this page you can find all versions of the php package f9webltd/laravel-api-response-helpers. 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-api-response-helpers

Run Tests - Current Packagist Version Total Downloads Packagist PHP Version Packagist License

Laravel API Response Helpers

A simple package allowing for consistent API responses throughout your Laravel application.

Requirements

Legacy Support

For PHP ^7.4 and Laravel ^6.0 / ^7.0 support, use package version ^1.5

Installation / Usage

Simply reference the required trait within your controller:

Optionally, the trait could be imported within a base controller.

Available methods

respondNotFound(string|Exception $message, ?string $key = 'error')

Returns a 404 HTTP status code, an exception object can optionally be passed.

respondWithSuccess(array|Arrayable|JsonSerializable|null $contents = null)

Returns a 200 HTTP status code, optionally $contents to return as json can be passed. By default returns ['success' => true].

respondOk(string $message)

Returns a 200 HTTP status code

respondUnAuthenticated(?string $message = null)

Returns a 401 HTTP status code

respondForbidden(?string $message = null)

Returns a 403 HTTP status code

respondError(?string $message = null)

Returns a 400 HTTP status code

respondCreated(array|Arrayable|JsonSerializable|null $data = null)

Returns a 201 HTTP status code, with response optional data

respondNoContent(array|Arrayable|JsonSerializable|null $data = null)

Returns a 204 HTTP status code, with optional response data. Strictly speaking, the response body should be empty. However, functionality to optionally return data was added to handle legacy projects. Within your own projects, you can simply call the method, omitting parameters, to generate a correct 204 response i.e. return $this->respondNoContent()

setDefaultSuccessResponse(?array $content = null): self

Optionally, replace the default ['success' => true] response returned by respondWithSuccess with $content. This method can be called from the constructor (to change default for all calls), a base API controller or place when required.

setDefaultSuccessResponse is a fluent method returning $this allows for chained methods calls:

Or

Use with additional object types

In addition to a plain PHP array, the following data types can be passed to relevant methods:

This allows a variety of object types to be passed and converted automatically.

Below are a few common object types that can be passed.

Laravel Collections - Illuminate\Support\Collection

Laravel Eloquent Collections - Illuminate\Database\Eloquent\Collection

Laravel API Resources - Illuminate\Http\Resources\Json\JsonResource

This package is intended to be used alongside Laravel's API resources and in no way replaces them.

Motivation

Ensure consistent JSON API responses throughout an application. The motivation was primarily based on a very old inherited Laravel project. The project contained a plethora of methods/structures used to return an error:

I wanted to add a simple trait that kept this consistent, in this case:

$this->respondError('Ouch')

Upgrading

Please see UPGRADING for details.

Contribution

Any ideas are welcome. Feel free to submit any issues or pull requests.

Testing

composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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


All versions of laravel-api-response-helpers with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
illuminate/support Version ^8.12|^9.0|^10.0|^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 f9webltd/laravel-api-response-helpers contains the following files

Loading the files please wait ....