2017 © Pedro Peláez
 

library laravel-4-jsend

Laravel 4 standard JSON response format

image

papasmurf/laravel-4-jsend

Laravel 4 standard JSON response format

  • Tuesday, June 2, 2015
  • by papasmurf
  • Repository
  • 1 Watchers
  • 3 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Standardized JSON response with JSend

According to OmniTI Labs @ http://labs.omniti.com/jsend:, (*1)

JSend is a specification that lays down some rules for how JSON responses from web servers should be formatted. JSend focuses on application-level (as opposed to protocol- or transport-level) messaging which makes it ideal for use in REST-style applications and APIs., (*2)

Laravel (4.2)

Installation

Install this package through Composer by editing your composer.json file to require papasmurf/laravel-4-jsend., (*3)

"papasmurf/laravel-4-jsend": "dev-master"

Update Composer from the Terminal / Command-line:, (*4)

composer update

Once done add the service provider to app/config/app.php, (*5)

'Papasmurf\JSend\JSendServiceProvider',

After that you are free to add the facade to your list of aliases in app/config/app.php, (*6)

'JSend' => 'Papasmurf\JSend\Facades\JSend',

All set and done! You can now respond with consistent JSON messages without having to think of the ideal JSON format..ever!, (*7)

Usage


function doStuff() { return JSend::success([ 'redirect' => URL::action('UsersController@index') ]); } function validateStuff() { return JSend::fail([ 'failed' => $validation->failedFields(), 'message' => $message ]); } function tryStuff() { try { stuff(); } catch (Exception $e) { return JSend::error('whoops!'); } return JSend::success(); }

Standalone

Standalone usage is possible as well and requires a Response object with a json method:, (*8)


require __DIR__ . '/src/JSend.php'; class Response { public function json($data) { return json_encode($data); } } $responder = new Response; $JSend = new Papasmurf\JSend\JSend($responder); echo $JSend->success([ 'redirect' => 'mydomain.com/something' ]);

The Versions

02/06 2015

dev-master

9999999-dev https://github.com/papa-smurf/laravel-4-jsend

Laravel 4 standard JSON response format

  Sources   Download

MIT

The Requires

  • php >=5.4

 

laravel php jsend