定制 teepluss/restable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

teepluss/restable

最新稳定版本:2.0.1

Composer 安装命令:

composer require teepluss/restable

包简介

Laravel RESTful API format

README 文档

README

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

统计信息

  • 总下载量: 67.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 74
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 70
  • Watchers: 4
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-10-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固