Download the PHP package tyloo/api-response-helpers without Composer

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

run-tests Packagist Version Packagist PHP Version Packagist License

API Response Helpers

A simple package allowing for consistent API responses throughout your PHP application (Symfony, Laravel).

Requirements

Installation / Usage

composer require tyloo/api-response-helpers

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 $contents = [])

Returns a 200 HTTP status code

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 $data = [])

Returns a 201 HTTP status code, with response optional data

respondNoContent(?array $data = [])

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()

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.

Motivation

Ensure consistent JSON API responses throughout an application. The motivation was primarily based on a very old inherited 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')

Contribution

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

Testing

vendor/bin/phpunit

Credits

License

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


All versions of api-response-helpers with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-json Version *
symfony/http-foundation Version ^5.3
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 tyloo/api-response-helpers contains the following files

Loading the files please wait ....