2017 © Pedro Peláez
 

library restable

Laravel RESTful API format

image

teepluss/restable

Laravel RESTful API format

  • Wednesday, June 10, 2015
  • by Teepluss
  • Repository
  • 4 Watchers
  • 74 Stars
  • 24,465 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 14 Forks
  • 4 Open issues
  • 6 Versions
  • 9 % Grown

The README.md

RESTful format response for Laravel

For Laravel 4, please use the v1.x branch!

Restable is a useful to create RESTful API response format that support multiple format, such as Json, XML Serialized, PHP., (*1)

Installation

To get the lastest version of Theme simply require it in your composer.json file., (*2)

"teepluss/restable": "dev-master"

You'll then need to run composer install to download it and have the autoloader updated., (*3)

Once Theme is installed you need to register the service provider with the application. Open up config/app.php and find the providers key., (*4)

'providers' => array(

    'Teepluss\Restable\RestableServiceProvider'

)

Restable also ships with a facade which provides the static syntax for creating collections. You can register the facade in the aliases key of your config/app.php file., (*5)

'aliases' => array(

    'Restable' => 'Teepluss\Restable\Facades\Restable'

)

Publish config using artisan CLI., (*6)

php artisan vendor:publish

Usage

API RESTful format., (*7)

Example:, (*8)


use Teepluss\Restable\Contracts\Restable; class ApiBlogsController extends BaseController { protected $rest; /** * Checking permission. * * @return Response */ public function __construct(Restable $rest) { $this->rest = $rest; if ( ! Input::get('secret') == '12345') { return $this->rest->unauthorized()->render(); } } /** * Display a listing of the resource. * * @return Response */ public function index() { // Set default response format. //$this->rest->setDefaultFormat('xml'); // Override format response. //return $this->rest->listing(Blog::paginate())->to('xml'); //return $this->rest->listing(Blog::paginate())->toXML(); return $this->rest->listing(Blog::paginate())->render(); } /** * Show the form for creating a new resource. * * @return Response */ public function create() { return View::make('api.blogs.create'); } /** * Store a newly created resource in storage. * * @return Response */ public function store() { $blog = new Blog; $validator = Validator::make(Input::all(), array( 'title' => 'required', 'description' => 'required' )); if ($validator->fails()) { return $this->rest->unprocess($validator)->render(); } $blog->title = Input::get('title'); $blog->description = Input::get('description'); $blog->save(); return $this->rest->created($blog)->render(); } /** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $blog = Blog::find($id); if ( ! $blog) { return $this->rest->missing()->render(); } return $this->rest->single($blog)->render(); } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { $blog = Blog::find($id); if ( ! $blog) { return $this->rest->missing()->render(); } return View::make('api.blogs.edit', compact('blog')); } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { $blog = Blog::find($id); if ( ! $blog) { return $this->rest->missing()->render(); } $validator = Validator::make(Input::all(), array( 'title' => 'required', 'description' => 'required' )); if ($validator->fails()) { return $this->rest->unprocess($validator)->render(); } $blog->title = Input::get('title'); $blog->description = Input::get('description'); $blog->save(); return $this->rest->updated($blog)->render(); } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $blog = Blog::find($id); if ( ! $blog) { return $this->rest->missing()->render(); } $blog->delete(); return $this->rest->deleted()->render(); } }

Error cases., (*9)

// Unauthorized.
Restable::unauthorized()->render();

// Bad request.
Restable::bad()->render();

// Missing, Not found.
Restable::missing()->render();

// Unprocess, Validation Failed.
Restable::unprocess()->render();

// Custom.
Restable::error(null, 429)->render();

Another success cases., (*10)

return Restable::success()->render();

Changing error code., (*11)

return Restable::code(9001)->bad('message')->render();

Render to another format., (*12)

// XML
return Restable::single($data)->render('xml');

// Serialized
return Restable::single($data)->render('serialized');

// PHP
return Restable::single($data)->render('php');

// JSON
return Restable::single($data)->render('json');

// JSONP
return Restable::single($data)->render('json', Input::get('callback'));
// OR
return Restable::single($data)->toJson(Input::get('callback'));

Support or Contact

If you have some problem, Contact teepluss@gmail.com, (*13)

Support via PayPal, (*14)

The Versions

10/06 2015

dev-master

9999999-dev https://github.com/teepluss/laravel-restable

Laravel RESTful API format

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

laravel api restful

21/03 2015

2.0.1

2.0.1.0 https://github.com/teepluss/laravel-restable

Laravel RESTful API format

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

laravel api restful

03/03 2015

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/teepluss/laravel-restable

Laravel4 RESTful API format

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel api restful laravel4

03/03 2015

2.0.0

2.0.0.0 https://github.com/teepluss/laravel-restable

Laravel RESTful API format

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

laravel api restful

03/03 2015

1.0.2

1.0.2.0 https://github.com/teepluss/laravel-restable

Laravel4 RESTful API format

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel api restful laravel4

12/01 2015

1.0.1

1.0.1.0 https://github.com/teepluss/laravel4-restable

Laravel4 RESTful API format

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel api restful laravel4