Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/ryandadeng/laravel-api-module-generator

(Pending for further development) A simple laravel api module service generator to provide you with a clean and lightweight code structure.
https://github.com/ryandadeng/laravel-api-module-generator

laravel-api-generator laravel-crud laravel-crud-generator laravel-maker laravel-module-generator laravel-service-generator

Last synced: 11 days ago
JSON representation

(Pending for further development) A simple laravel api module service generator to provide you with a clean and lightweight code structure.

Awesome Lists containing this project

README

        

# Laravel Api Module Generator

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]

## IMPORTANT UPDATE

This project is not finished yet, its still missing a lot features. However, you can still use it if you just want a simple structure. I will slowly update the project in the following days.

-----

This is a lightweight module-based boilerplate generator.

Please be mind, if you have any issues related to this package, please submit tickets I will only update if there is bugs.

## DESCRIPTION

A simple laravel api module service generator to provide you with a clean and lightweight code structure.

It's just simple and NO other third party helper code.

IMPORTANT: This package is not aimed at setting up a module, this is only for you to quickly create a structure and make it as your own starting point.

## Demo
(The Demo might be slightly different to your actual result)

![](https://github.com/RyanDaDeng/design-patterns/blob/master/apimodule.gif)

|-- Module
|-- Contracts
|-- Controllers
|-- Api
|-- V1
|-- Web
|-- Facades
|-- Models
|-- Providers
|-- Repositories
|-- Requests
|-- Api
|-- V1
|-- Web
|-- Services
|-- routes

## Installation

- Laravel > 5.4

Via Composer, put it into composer require-dev list, its not needed for production

``` bash
$ composer require timehunter/laravel-api-module-generator --dev
```

Publish config

``` bash
php artisan vendor:publish --provider="TimeHunter\LaravelApiModuleGenerator\LaravelApiModuleGeneratorServiceProvider"
```


## Usage

Step 1 - make up your own config - laravelapimodulegenerator.php in config folder

Step 2 - run command:
``` bash
php artisan laravel-api-module:make
```

Step 3 - add your service provider and facade from generated folder

Step 4 - run command to see registered routes
``` bash
php artisan route:list
```

Step 5: Play with your module

## About Folder Structure

I try to make the structure as small as possible, there can be many extensions possibly added in. However, people usually has their own habit to create their own structure.

You can also check [laravel-file-generator](https://github.com/RyanDaDeng/laravel-file-generator) project to create your own structure if you want.

### Repository-Service Pattern vs Laravel Eloquent
There is a debate about whether to implement the repository-service pattern in laravel or not...well, after reading a bunch of blogs, articles and my years' experiences, I can surely tell you Laravel is not designed for using PURE repository-service pattern like Java/Spring.

One big concern is that, Eloquent model uses Active Record which is completely different from Data Mapper. If you are still deciding to make PURE repository-service pattern in Laravel, you will end up with a nightmare, massive, duplicate and ugly code as you are re-writing Eloquent model again.

However, this package still generates a Repository folder that is simply just a query collection, nothing else. I do still prefer you put all your Eloquent-like queries into one single class, it's not about technical SOLD principle levels, its only about code reusability and arrangement.

You can google and read blogs from online, there are some open-source projects available that people try to apply repository-service pattern in Laravel...yeah, very complicated and...my question was like why do you use Laravel in the first place..?

Sometimes you might do CACHE on your queries, you can check Decorator design pattern to help you set up.

### Facade Service

The Facade service is one of powerful Laravel features. (note: Laravel's Facade class is not standard Facade design pattern as we knew).

The sturcture takes repositories(query collection) as paramters, so all you have to do is that put all your business logic in the Service class. Also, all your queires go to Repository. You might notice that there is an interface attached to Service, this is only easier for you to quickly generate service functions, you can remove it if you want.

One endpoint by default would be mapped with one service function right now.

### Models

As the structure is module-based, I prefer you bind $table and $connection for every models in each module folder.

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## License

license. Please see the [license file](license.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/timehunter/laravel-api-module-generator.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/timehunter/laravel-api-module-generator.svg?style=flat-square
[link-packagist]: https://packagist.org/packages/timehunter/laravel-api-module-generator
[link-downloads]: https://packagist.org/packages/timehunter/laravel-api-module-generator