- Dingo API
- Darkaonline swagger (Documentation)
-
Clone repository.
git clone https://github.com/tajulasri/laravel-api-boilerplate.git
-
Run composer install.
composer install
-
Run migrations and preseed data seeding.
php artisan migrate --seed
-
Setup .env file for your database connection
-
Run
php artisan serve
-
Request via curl
curl -X POST localhost:8000/api/auth/login -d [email protected] -d password=password
This boilerplate is using swagger ui for API documentation and please refer swagger php thou. Documentation for local can be found using this url.
http://localhost:8000/api/documentation
Please refer this package documentation https://github.com/DarkaOnLine/L5-Swagger
- Generate transformers with attributes.
- Generate CRUD API controller with models supplied.
- As per said you still need to setup database manually for attributes else you already have existing database structure might be can use this package. https://github.com/reliese/laravel. Its help you to generate model based on existing database.
php artisan make:transformer ExampleUserTransformer -m User
This command will find attributes inside current table based on those models and put it into transformer.
<?php
namespace App\Http\Transformers;
use App\Entity\User;
use League\Fractal\TransformerAbstract;
class ExampleUserTransformer extends TransformerAbstract
{
public function transform(User $user)
{
return [
'id' => $user->id,
'company_id' => $user->company_id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'password' => $user->password,
'mobile' => $user->mobile,
'activated' => $user->activated,
'first_time_login' => $user->first_time_login,
'login_counter' => $user->login_counter,
'avatar' => $user->avatar,
'remember_token' => $user->remember_token,
'created_at' => $user->created_at,
'updated_at' => $user->updated_at,
];
}
}
Eg: Generated tranformer based on model
We have models and transformer and now we need some controller with CRUD operation into it.
Generating API controller can be done using this command and don't forget to supply transformer and model into it and its will be use as default transformer in that controller.
php artisan api:crud Products/ManageProductController -m User -t ExampleUserTransformer
Specify version
php artisan api:crud Products/ManageProductController -m User -t ExampleUserTransformer --api-version=v1
Result generated controller
<?php
namespace App\Http\Controllers\Api;
use App\Entity\User;
use App\Http\Controllers\Controller;
use App\Http\Transformers\UserTransformer;
use Illuminate\Http\Request;
class ExampleApiUserController extends Controller
{
/**
* model
* @var [type]
*/
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* @SWG\Get(
* path="/user",
* summary="",
* method="get",
* tags={"user"},
* description="",
* operationId="index",
* produces={"application/json"},
* @SWG\Response(response="200", description="")
* )
* @param Request $request [description]
* @return [type] [description]
*/
public function index()
{
return $this->response
->collection(
$this->user->get(),
new UserTransformer
);
}
/**
* @SWG\Post(
* path="/user",
* summary="",
* method="post",
* tags={"user"},
* description="",
* operationId="store",
* produces={"application/json"},
* @SWG\Response(response="200", description="")
* )
**/
public function store(Request $request)
{
$user = $this->user->firstOrcreate($request->except('_token'));
return $this->response->item($user,new UserTransformer);
}
/**
* @SWG\Get(
* path="/user/{id}",
* summary="",
* method="get",
* tags={"user"},
* description="",
* operationId="show",
* produces={"application/json"},
* @SWG\Parameter(in="path",name="id",required=true,type="integer"),
* @SWG\Response(response="200", description="")
* )
**/
public function show($id)
{
$user = $this->user->find($id);
return $this->response->item($user,new UserTransformer);
}
/**
* @SWG\Put(
* path="/user/{id}/update",
* summary="",
* method="put",
* tags={"user"},
* description="",
* operationId="update",
* produces={"application/json"},
* @SWG\Parameter(in="path",name="id",required=true,type="integer"),
* @SWG\Response(response="200", description="")
* )
**/
public function update(Request $request, $id)
{
$user = $this->user->find($id);
$user->update($request->except('_token'));
return $this->response->item($user,new UserTransformer);
}
/**
* @SWG\Delete(
* path="/user/{id}/delete",
* summary="",
* method="delete",
* tags={"user"},
* description="",
* operationId="destroy",
* produces={"application/json"},
* @SWG\Parameter(in="path",name="id",required=true,type="integer"),
* @SWG\Response(response="200", description="")
* )
**/
public function destroy($id)
{
$user = $this->user->find($id);
$category->delete();
return $this->response->noContent();
}
}
Dont forget to update routes in your api.php
files.
Lastly update API documentations by using this command.
php artisan l5-swagger:generate
Feel free to contribute.