Skip to content

Latest commit

 

History

History
127 lines (94 loc) · 4.81 KB

EN_README.md

File metadata and controls

127 lines (94 loc) · 4.81 KB

lumen-api-demo

Forgive my poor English.

This is a demo for lumen 5.7. if you are using lumen to write REST api it will help you.

This demo use dingo/api tymon/jwt-auth and write some easy APIs and PHPUNIT

lumen5.x please look appropriate branch

USEFUL LINK

USAGE

$ git clone git@github.com:liyu001989/lumen-api-demo.git
$ composer install
$ cp .env.example .env
$ vim .env
        DB_*
            config  uration your database
	    JWT_SECRET
            php artisan jwt:secret
	    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
$ php artisan db:seed
$ generate api doc like this "apidoc -i App/Http/Controller/Api/v1 -o public/apidoc"

REST API DESIGN

just a demo for rest api design

    demo: user, post, comment
    get    /api/posts              	 post index
    post   /api/posts              	 create a post
    get    /api/posts/5            	 post detail
    put    /api/posts/5            	 replace a post
    patch  /api/posts/5            	 update part of a post
    delete /api/posts/5            	 delete a post
    get    /api/posts/5/comments     comment list of a post
    post   /api/posts/5/comments     add a comment
    get    /api/posts/5/comments/8   comment detail
    put    /api/posts/5/comments/8   replace a comment
    patch  /api/posts/5/comments/8   update part of a comment
    delete /api/posts/5/comments/8   delete a comment
    get    /api/users/4/posts        post list of a user
    get    /api/user/posts           post list of current user

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 user transformer

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

maybe you can knowstand with this links:

transformer data serizlizer

dingo/api use Fractal to transformer resouses,fractal provider 3 serializer,Array,DataArray,JsonApi.more details at here 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.

TODO

  • phpunit

Tips

  • dingo/api#672 transformer include
  • if there is a space in env file, it will throw log not found error

License

MIT license