timehunter/laravel-api-module-generator

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

dev-master 2018-12-27 02:17 UTC

This package is auto-updated.

Last update: 2024-10-27 15:27:21 UTC


README

Latest Version on Packagist Total 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)

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

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

Publish config

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:

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

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 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 ryandadeng@gmail.com instead of using the issue tracker.

License

license. Please see the license file for more information.