For large scale mobile and Frontend JS aps, there is a need for order and sophistication. Citrus Provides that. For Laravel users, this package is a wrapper and a formatter for backend data to be passed as either arrays or JSON. Citrus takes Laravel object data and builds it into arrays with custom indexes. Then, it will then return the data in a standard format, that never changes so that devs relying on it are never confused. success, error, data
are the bread and butter of Citrus.
Citrus can be installed easily, by adding the following line to your composer.json
file.
"wiretech/citrus": "dev-master"
After this, you will need to add the following lines to your providers and alias arrays respectively in your app/config/app.php
file.
--- providers ---
'Wiretech\Citrus\CitrusServiceProvider'
--- alias ---
'Citrus'=>'Wiretech\Citrus\Classes\Citrus'
Then, run composer update
followed by composer install
to install the package
Citrus is lightweight and is easily customizable. If you see something you want in it, add it! That's the beauty of it. The code is commented and easy to understand, so anyone can jump right in.
If there is a problem, just let me know. I will be happy to assist you. My contact information is under the contact section, but here is my email again. wiretech@gmail.com
As well as the person to person contact, the docs are a great way to learn and understand Citrus
This is the main function of Citrus
Param | Required | Type | Default | Description |
---|---|---|---|---|
success |
false |
boolean |
false |
Returns whether the request was successful or not |
error |
false |
string |
'An unknown error occurred' |
Returns the error message. Exception messages must be thrown manually |
data |
false |
array |
null |
Data that you want to be returned |
Citrus
meaning that all responses are generated from this function
Citrus::response()
This will return the following JSON
{
"success": 0,
"error": "An unkown error occured",
"data": null
}
If we want to return a successful response, we would set the success
to true
Citrus::response(1, null, $data)
This wil return the following JSON
{
"success": 1,
"error": null,
"data": {
"message": $foo
}
}
This is the secondary function of Citrus
This function takes the data you have - whether it be array or object - and converts it into an array with a custom index. Great thing about this function: It's completely scalable. You can convert all of your data into one multidimensional array, with certain objects stored at custom indexes.
Param | Required | Type | Default | Description |
---|---|---|---|---|
index |
true |
string |
N/A |
Defines the index at which your array will be created and stored |
array |
true |
array |
N/A |
The array you want stored |
combine()
function is scalable. However, for it to work properly, it is imperative that you think of the index
and the array
as a pair. This function can accept an odd number of arguments, but it will ignore a trailing argument without its pair.
Citrus::combine('user', $user);
This will build an array with an index user
where the data $user
will be stored. After building the array, the function will then return the following JSON.
{
"success": 1,
"error": null,
"data": {
"user": $user
}
}
We can also specify multiple arrays and indexes to be built. Each array and index pair must come in twos, so that if you want to define multiple indexes and data to be stored at index, it should look like this.
Citrus::combine($index, $foo, $index2, $foo2 ....);
An example of this is as follows.
Citrus::combine('user', $user, 'message', $foo);
The responding JSON will look like this
{
"success": 1,
"error": null,
"data": {
"user": $user,
"message": $foo
}
}