Skip to content

Instantly share code, notes, and snippets.

@18601673727
Created July 10, 2014 08:33
Show Gist options
  • Save 18601673727/f397688dddaff1128eaf to your computer and use it in GitHub Desktop.
Save 18601673727/f397688dddaff1128eaf to your computer and use it in GitHub Desktop.
An laravel resource controller integrated with Dingo\Api and League\Fractal
<?php
class PostsController extends Controller
{
protected $api;
protected $auth;
public function __construct(Dingo\Api\Dispatcher $api, Dingo\Api\Auth\Shield $auth)
{
$this->api = $api;
$this->auth = $auth;
}
/**
* Display a listing of the resource.
* GET /posts
*
* @return Response
*/
public function index()
{
// $this->errorNotFound();output
// Input::get('limit') or die('123');
return Response::api()->withCollection(Post::all(), new PostTransformer);
}
/**
* Show the form for creating a new resource.
* GET /posts/create
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
* POST /posts
*
* @return Response
*/
public function store()
{
$post = new Post;
$post->title = Input::get('title');
$post->content = Input::get('content');
$post->user_id = Input::get('user_id');
if ($post->save()) {
return [
'status' => 'Done',
'message' => 'Your request has been succeed!',
'result' => [
'id' => $post->id
]
];
} else {
throw new Dingo\Api\Exception\StoreResourceFailedException('');
}
}
/**
* Display the specified resource.
* GET /posts/{id}
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
* GET /posts/{id}/edit
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
* PUT /posts/{id}
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
* DELETE /posts/{id}
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment