Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/marco-gallegos/lumen-rest-api-template

Template / Boilerplate To Build Rest API's with Lumen 6
https://github.com/marco-gallegos/lumen-rest-api-template

api api-template boilerplate laravel laravel-framework laravel-template lumen lumen-framework php php-template php7 rest rest-api rest-template restapi template

Last synced: about 4 hours ago
JSON representation

Template / Boilerplate To Build Rest API's with Lumen 6

Awesome Lists containing this project

README

        

# Lumen API Template/Boilerplate with JWT Auth

[![License](https://img.shields.io/github/license/marco-gallegos/lumen-rest-api-template?style=for-the-badge)](http://opensource.org/licenses/MIT)
![PHP8](https://img.shields.io/badge/PHP-8-blue?style=for-the-badge)
![LUMEN8.X](https://img.shields.io/badge/LUMEN-8.X-orange?style=for-the-badge)

This is a boilerplate for lumen 8.x if you are using lumen to write REST api it will help you.

This project use:

* [dingo/api](https://github.com/dingo/api)
* [tymon/jwt-auth](https://github.com/tymondesigns/jwt-auth)
* [palanik/lumen-cors](https://github.com/palanik/lumen-cors)
* [flipboxstudio/lumen-generator](https://github.com/flipboxstudio/lumen-generator)

Write some easy APIs.

Checkout my other repo that is focused on reply to this template but using graphql [lumen-graphql-api-template](https://github.com/marco-gallegos/lumen-graphql-api-template)

Feel free to make a pull request and make this repo better :D

## TODO

- [ ] docker
- [ ] phpunit validation

## Main Features

### Document your API

```bash
apidoc -i App/Http/Controller/Api/v1 -o public/apidoc
```

### Use Artisan Generators

By default lumen does't support the useful laravel/artisan generators and using [this extension](https://github.com/flipboxstudio/lumen-generator) we can use this comands executing a "dev inclusive" composer install.

Test:

```bash
php artisan make:model MyModel
```

###

## USEFUL LINKS

- dingo/api [https://github.com/dingo/api](https://github.com/dingo/api)
- json-web-token(jwt) [https://github.com/tymondesigns/jwt-auth](https://github.com/tymondesigns/jwt-auth)
- transformer [fractal](http://fractal.thephpleague.com/)
- apidoc [apidocjs](http://apidocjs.com/)
- Rest API guidance [jsonapi.org](http://jsonapi.org/format/)
- Rest API Debug [Insomnia](https://insomnia.rest/)
- A good article [http://oomusou.io/laravel/laravel-architecture](http://oomusou.io/laravel/laravel-architecture/)

## Installation

### 1 Install the project

#### Using GIT

``` bash
git clone https://github.com/marco-gallegos/lumen-rest-api-template.git new_api
cd new_api
composer install
cp .env.example .env
php artisan jwt:secret
```

#### Using Composer

```bash
composer create-project --stability dev cbxtechcorp/lumen-rest-api-template new_api
```

### 2 configre your project

Now give to your project access tou your database and create the users table whit a test user.

```bash
vim .env
DB_*
configure your database access

APP_KEY
key:generate is abandoned in lumen, so do it yourself
md5(uniqid()),str_random(32) etc.,maybe use jwt:secret and copy it

php artisan migrate --seed
```

## Documentation

### Serve Your API

```bash
php artisan serve
# or
php -S localhost:8000 -t public
```

### Deploy

In a production server we can omit development packages autoload using this command

```bash
composer install --no-dev
```

### Routes

We have some routes to start.

- api/v1/users
- api/v1/permission

## FAQ

About JWT

There is no session and auth guard in lumen 5.2, so attention `config/auth.php`. Also user model must implement `Tymon\JWTAuth\Contracts\JWTSubject`

How to use mail

- composer require `illuminate/mail` and `guzzlehttp/guzzle`
- register email service in `bootstrap/app.php` or `some provider`
- add `mail.php` `services.php` in config, just copy them from laravel
- add `MAIL_DRIVER` in env

How to use transformer

transformer is a layer help you format you resource and their relationship.

maybe you can knowstand with this links:

- [https://lumen-new.lyyw.info/api/posts](https://lumen-new.lyyw.info/api/posts)
- [https://lumen-new.lyyw.info/api/posts?include=user](https://lumen-new.lyyw.info/api/posts?include=user)
- [https://lumen-new.lyyw.info/api/posts?include=user,comments](https://lumen-new.lyyw.info/api/posts?include=user,comments)
- [https://lumen-new.lyyw.info/api/posts?include=user,comments:limit(1)](https://lumen-new.lyyw.info/api/posts?include=user,comments:limit(1))
- [https://lumen-new.lyyw.info/api/posts?include=user,comments.user](https://lumen-new.lyyw.info/api/posts?include=user,comments.user)
- [https://lumen-new.lyyw.info/api/posts?include=user,comments:limit(1),comments.user](https://lumen-new.lyyw.info/api/posts?include=user,comments:limit(1),comments.user)

transformer data serizlizer

dingo/api use [Fractal](http://fractal.thephpleague.com/) to transformer resouses,fractal provider 3 serializer,Array,DataArray,JsonApi.more details at here [http://fractal.thephpleague.com/serializers/](http://fractal.thephpleague.com/serializers/)。DataArray is default.You can set your own serizlizer like this:

see bootstrap/app.php
$app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
$fractal = new League\Fractal\Manager;
// $serializer = new League\Fractal\Serializer\JsonApiSerializer();
$serializer = new League\Fractal\Serializer\ArraySerializer();
// $serializer = new App\Serializers\NoDataArraySerializer();
$fractal->setSerializer($serializer);,
return new Dingo\Api\Transformer\Adapter\Fractal($fractal);
});

I think default DataArray is good enough.

## Check the Original Project

[Lumen API Demo](https://github.com/liyu001989/lumen-api-demo)