A starter project to develop API with Lumen 5.7 (Updated).
This project has been rewritten from scratch. If you are looking for the previous version which was made for Lumen v5.4 please use this link or navigate to branch v5.4
.
- spatie/laravel-fractal@^5.4
- tymon/jwt-auth@1.0.0-rc.3
- spatie/laravel-query-builder@^1.12
- flipbox/lumen-generator@^5.6
- Clone the Repo:
git clone git@github.com:munza/lumen-api-starter.git
git clone https://github.com/munza/lumen-api-starter.git
cd lumen-api-starter
composer create-project
php artisan key:generate
php artisan jwt:secret
php artisan migrate
php artisan serve
php artisan ti
factory('App\Models\User')->create(['email' => 'admin@localtest.me', 'password' => 'secret'])
- Edit
.env
file for database connection configuration. - Edit the files located under
config
directory for configuration.
-
Add endpoint in
routes/web.php
.$router->group(['middleware' => 'auth:api'], function ($router) { $app->get('/users', 'UserController@index'); });
-
Add controller with
php artisan make:controller {name}
command -
Add model at
php artisan make:model {name}
. You can use-m
flag to add migration file and-f
flag for factory file. -
Add service at
app
directory.<?php namespace App; class Accounts { // Add service methods. }
-
Load the service in controller.
<?php namespace App\Http\Controllers; use App\Accounts; class UserController extends Controller { /** * Controller constructor. * * @param \App\Accounts $accounts */ public function __construct(Accounts $accounts) { $this->accounts = $accounts; } // Add controller methods. }
-
Add transformers at
app/Transformers
directory or use the commandphp artisan make:transformer {name}
.<?php namespace App\Transformers; use App\User; use League\Fractal\TransformerAbstract; class UserTransformer extends TransformerAbstract { /** * Transform object to array. * * @param \App\User $user * @return array */ public function transform(User $user): array { return [ 'id' => (int) $user->id, 'email' => (string) $user->email, ]; } }
-
Render JSON in controllers
<?php namespace App\Http\Controllers; use App\Accounts; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; class UserController extends Controller { /** * Controller constructor. * * @param \App\Accounts $accounts */ public function __construct(Accounts $accounts) { $this->accounts = $accounts; } /** * List of all users. * * @return \Illuminate\Http\JsonResponse */ public function index(): JsonResponse { $users = $this->accounts->getUsersWithPagination($request); return response()->json($users, Response::HTTP_OK); } }
-
Exception message, status code and details can now be easily displayed by declaring these as methods in an exception class.
<?php namespace App\Exceptions; use Symfony\Component\HttpKernel\Exception\HttpException; class CustomException extends HttpException { public function getMessage(): string { return 'Custom message'; } public function getStatusCode(): int { return 500; } public function getDetails(): ?array { return []; } }
- Remove the customization feature from error trait.
- Move all the extended features inside a package.
- Add the feature to use a transformer for error response.
Please create an issue if you find any bug or error.
Feel free to make a pull request if you want to add anything.
MIT