teepluss / laravel-restable by teepluss

Laravel RESTful API format
64,584
70
5
Package Data
Maintainer Username: teepluss
Maintainer Contact: admin@laravel.in.th (Teepluss)
Package Create Date: 2013-10-28
Package Last Update: 2016-06-09
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-14 15:01:57
Package Statistics
Total Downloads: 64,584
Monthly Downloads: 205
Daily Downloads: 5
Total Stars: 70
Total Watchers: 5
Total Forks: 11
Total Open Issues: 4

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.

Installation

To get the lastest version of Theme simply require it in your composer.json file.

"teepluss/restable": "dev-master"

You'll then need to run composer install to download it and have the autoloader updated.

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

'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.

'aliases' => array(

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

)

Publish config using artisan CLI.

php artisan vendor:publish

Usage

API RESTful format.

Example:


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.

// 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.

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

Changing error code.

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

Render to another format.

// 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

Support via PayPal