teepluss / restable
Laravel RESTful API格式
2.0.1
2015-03-21 18:00 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-14 15:05:59 UTC
README
对于Laravel 4,请使用v1.x分支!
Restable是一个用于创建支持多种格式(如Json、XML序列化、PHP)的RESTful API响应格式的实用工具。
安装
要获取最新版本的Theme,请在您的composer.json
文件中添加它。
"teepluss/restable": "dev-master"
然后您需要运行composer install
来下载它并更新自动加载器。
一旦Theme安装完成,您需要将服务提供者注册到应用程序中。打开config/app.php
并找到providers
键。
'providers' => array(
'Teepluss\Restable\RestableServiceProvider'
)
Restable还包含一个门面,它提供了创建集合的静态语法。您可以在config/app.php
文件的aliases
键中注册门面。
'aliases' => array(
'Restable' => 'Teepluss\Restable\Facades\Restable'
)
使用Artisan CLI发布配置。
php artisan vendor:publish
用法
API RESTful格式。
示例
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(); } }
错误情况。
// 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();
其他成功情况。
return Restable::success()->render();
更改错误代码。
return Restable::code(9001)->bad('message')->render();
渲染到其他格式。
// 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'));
支持或联系
如果您遇到任何问题,请联系teepluss@gmail.com