Skip to content

This is to create easy API responses for my projects. Yes, I'm a little bit lazy to repeat the same code all the time. Thanks for Composer for make easy code in PHP. XD Yes, I will change this in the feature! : )

License

Notifications You must be signed in to change notification settings

AGrandesR/global-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Global Response for PHP

This is to create easy API responses for my projects. Yes, I'm a little bit lazy to repeat the same code all the time. Thanks for Composer for make easy code in PHP. XD Yes, I will change this in the feature! : )

Prerequisites 📋

You will need to have installed composer in your computer.

Instalation 🔧

You need to require the package to your project.

composer require agrandesr/response

Next, you can use in your code the two classes Response and GlobalResponse, but ignore the first. I recomend to you (and me) use only GlobalResponse, it is more cool and you can use everywhere in your code... Without instantiation! Sorry for my english... : /

To use only have to use at the start of your thread. But I'm sure that you are doing! ;)

require './vendor/autoload.php';

Next, to use only need to use the GlobalResponse in the place that you want to use!

use AgrandesR\GlobalResponse;
//Or if you want an alias
use AgrandesR\GlobalResponse as Response;
//Or more funny..
use AgrandesR\GlobalResponse as CoolResponse; // ji ji ji 😝

How to start 🚀

The idea of the Global Response is to make easy the response of code to client in APIs created in PHP. The first version is created to JSON APIs, but my idea is to improve this class with my other composer package A.G.R (api-generator-router).

The only that you need is to worry about the logic. GlobalResponse give to you and standard of response for each project that you use. You only have to worry about the logic of the code, not minor issues like how to return data.

At the same time, you can add warnings and errors to response in a easy way to have an easy response.

¡Whoop! But, how to start? 🤔

You only need to read quickly the below methods to have an idea of how to use. There are basicaly two type of functions. The functions that add information to your response and the render functions (show and showData).

Please, read is very easy. All are static methods and you can use in diferrent files without thing in how to share the Response object. When you want to finish your thread and send response, only use one of the methods created for that.

Show functions

First, start with the end. When you want to print the response is very easy. Only need to use one of the follow functions.

Show

GlobalResponse::show();

Example of response without data or meta:

{
    "success":true,
    "code":200
}

Show Data

The showData function only show the content of the data. This means that all the warnings, the status code or the meta of the response is hidden for the client user.

GlobalResponse::addData('key','value');

GlobalResponse::showData();

Example of only data response:

{
    "key":"value"
}

EXTRA


Versioning: 📌

For all available versions, see the tags in this repository.

Autores ✒️

Menciona a todos aquellos que ayudaron a levantar el proyecto desde sus inicios

License 📄

This project is under the License MIT - read the file LICENSE for more details.

Thanks to: 🎁

About

This is to create easy API responses for my projects. Yes, I'm a little bit lazy to repeat the same code all the time. Thanks for Composer for make easy code in PHP. XD Yes, I will change this in the feature! : )

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages