GithubHelp home page GithubHelp logo

laravel-api-boilerplate's Introduction

Laravel API Boilerplate using (Laravel 5.5)

Default functionality packages

  • Dingo API
  • Darkaonline swagger (Documentation)

Installation

  1. Clone repository. git clone https://github.com/tajulasri/laravel-api-boilerplate.git

  2. Run composer install. composer install

  3. Run migrations and preseed data seeding. php artisan migrate --seed

  4. Setup .env file for your database connection

  5. Run php artisan serve

  6. Request via curl curl -X POST localhost:8000/api/auth/login -d [email protected] -d password=password

Documentations

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

Command availables

  • Generate transformers with attributes.
  • Generate CRUD API controller with models supplied.

Suggestion

  • 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.

Generate transformer based on models

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();
    }
}

IMPORTANT

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.

laravel-api-boilerplate's People

Contributors

tajulasri avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

idiey

laravel-api-boilerplate's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.