A Laravel response helper methods.

Overview

A Laravel response helper methods. The package respond provides a fluent syntax to form array or json responses.
In its configuration file, it allows the addition of custom methods.

Installation

You can install the package via composer:

composer require nnjeim/respond

Configuration

php artisan vendor:publish --provider="Nnjeim\Respond\RespondServiceProvider"

The respond.php config file allows:

  • The presetting of the response format array/json.
  • The possibility to edit the response parameters.
  • The possibility to add custom methods.

Usage

Respond Facade
use Nnjeim\Fetch\Fetch;
use Nnjeim\Respond\Respond;

['response' => $response, 'status' => $status] = Fetch::setBaseUri('https://someapi.com/')->get('countries');

	if ($status === 200 && $response->success) {

		return Respond::toJson()
			->setMessage('countries')
			->setData($response->data)
			->withSuccess();
	}

	abort(400);
RespondHelper Instantiation
use Nnjeim\Respond\RespondHelper;

private RespondHelper $respond;
private array $data;
private bool $success;

public function __construct(RespondHelper $respond)
{
	$this->respond = $respond;
}
.
.
.
$respond = $this
		->respond
		->toJson()
		->setMessage('countries')
		->setData($data);

if ($this->success)
{
	return $respond->withSuccess()
}

return $respond->withErrors();

Methods

Set the status code
Sets the response status code  

@return $this       setStatusCode(int $statusCode)
Set the message
Sets the response title message

@return $this       setMessage(string $message)
Set the meta data
Sets the response meta data. The meta data will be merged with the response data array.

@return $this       setMeta(array $meta)
Set the data
Sets the response data array.

@return $this       setData(array $data)
Set the errors
Sets the response errors.

@return $this       setErrors(array $errors)
respond in Json format
returns an instance of Illuminate\Http\JsonResponse  

this method overwrites the config `toJson` set value.

@return $this       toJson()
Respond with success
On success response. The default response status code is 200.   

@return array|JsonResponse       withSuccess()
Respond with created
On created response. The default response status code is 201.   

@return array|JsonResponse       withCreated()
Respond with accepted
On accepted response. The default response status code is 202.   

@return array|JsonResponse       withAccepted()
Respond with no content
On success response with no results found. The default status code is 204

@return array|JsonResponse       withNoContent()
Respond with errors
On error response. The default response status code is 422.   

@return array|JsonResponse       withErrors(?array $errors = null)
Respond with server error
On server error response. The default response status code is 500.   

@return array|JsonResponse       withServerError()       
Respond with not found
Record not found error. The default response status code is 404.

@return array|JsonResponse       withNotFound()
Respond with not authenticated
Not authenticated reponse. The default response status code is 401.

@return array|JsonResponse       withNotAuthenticated()
Respond with not authorized
Not authorized reponse. The default response status code is 403.

@return array|JsonResponse       withNotAuthorized()

Respond

@return array|JsonResponse

	[
		'response' => [
			'success' => true,
			'message' => 'message',
			'data' => [],
			'errors' => [],
		],
		'status' => 200,
	];

Custom methods

In the respond.php config file, in the responses array add an array entry where the key is name of the method in lower case and the value contains the desired success, message and status params.

//example
'methodnotallowed' => [
    'success' => false,
    'message' => 'the method not allowed!',
    'status' => Response::HTTP_METHOD_NOT_ALLOWED,
],

Usage

Respond::withMethodNotAllowed();

Testing

The helpers and methods are tested with 99% coverage.
To run the tests.

composer install
composer test

To run the coverage test.

composer test-coverage

Changelog

Please see CHANGELOG for more information what has changed recently.

You might also like...
A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

A Magento Incident Response Plan Template

A Magento centric Incident Response Plan Template Introduction This will provide you with our defined process and procedures to use when responding to

Simple PSR-7 compatible response sender

Simple PSR-7 compatible response sender

Simple package to handle response properly in your API.

Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book.

Quickly identify controller methods with no route in your Laravel applications.
Quickly identify controller methods with no route in your Laravel applications.

Orphan Controller Quickly identify controller methods with no route in your Laravel applications. Installation You can install the package via Compose

Source code behind the Laracasts Larabit: My Favorite Laravel Collections Methods

My Favorite Laravel Collections Methods This is the source code behind the Laracasts Larabit: My Favorite Laravel Collections Methods, and features al

Control frontend access to properties/methods in Livewire using PHP 8 attributes.
Control frontend access to properties/methods in Livewire using PHP 8 attributes.

This package adds PHP 8.0 attribute support to Livewire. In specific, the attributes are used for flagging component properties and methods as frontend-accessible.

This package provides convenient methods for making token code, sending and verifying mobile phone verification requests.
This package provides convenient methods for making token code, sending and verifying mobile phone verification requests.

Laravel Mobile Verification Introduction Many web applications require users to verify their mobile phone numbers before using the application. Rather

A package that helps to group methods that mostly use for the view presentation purpose.

A package that helps to group methods that mostly use for the view presentation purpose form models to a dedicated presenter class.

Releases(1.2.1)
Owner
Najm Njeim
Najm Njeim
An opinionated support package for Laravel, that provides flexible and reusable helper methods and traits for commonly used functionality.

Support An opinionated support package for Laravel, that provides flexible and reusable helper methods and traits for commonly used functionality. Ins

Ian Olson 3 Apr 14, 2021
A helper package to flash a bootstrap alert to the browser via a Facade or a helper function.

Alert Box (Laravel) A helper package to flash a bootstrap alert to the browser via a Facade or a helper function. <div class="alert alert-info fade in

Ben-Piet O'Callaghan 17 Dec 30, 2022
Cache-purge-helper - Additional instances where nginx-helper and lscache plugin should be purged.

cache-purge-helper Additional instances where nginx-helper and lscache plugin should be purged. Install Extract the zip file. Upload them to /wp-conte

Jordan 10 Oct 5, 2022
Easily capture every incoming request and the corresponding outgoing response in your Laravel app.

Easily capture every incoming request and the corresponding outgoing response in your Laravel app. This package is designed to work only with the Lara

Mark Townsend 22 Nov 15, 2022
This Package helps you in laravel application to log all desired activity for each request from request entry point to generate response at a single snapshot.

Laravel Scenario Logger This Package helps you in laravel application to log all desired activity for each request from request entry point to generat

Mehrdad Mahdian 6 Sep 27, 2021
Convert remote api response data into laravel model

laravel remote model Create remote driver to convert remote api request into laravel model. 中文文档 日本語文書 overview Install the version between laravel5.5

张子彬 15 Aug 11, 2022
Simple and ready to use API response wrapper for Laravel.

Laravel API Response Simple Laravel API response wrapper. Installation Install the package through composer: $ composer require obiefy/api-response Re

Obay Hamed 155 Dec 14, 2022
Provides a powerful error response system for Laravel

Laravel Exceptions Laravel Exceptions was created by, and is maintained by Graham Campbell, and provides a powerful error response system for both dev

Graham Campbell 571 Jan 31, 2022
Laravel Custom Response Messages from Passport Oauth2 Server

This is a sample repository showing how to install Oauth2 server using Laravel's passport package and customize its responses.

M. Ismail 7 Nov 22, 2022
Laravel Response Formatter

I created this package to make it easier to format the response from a controller. I have used this package in my projects and I hope you enjoy it!

aris wahyudiyanto 12 Dec 5, 2022