Giter Club home page Giter Club logo

burhanahmed92 / laravel-repository-with-service Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yaza-putu/laravel-repository-with-service

0.0 0.0 0.0 91 KB

With repository and service you can separate business logic and query logic, slim controller and DRY. Simple generate repository and service with artisan command, automatically bind interface to class implement with IOC container and dependecy injection (SOLID)

License: MIT License

Shell 7.97% PHP 92.03%

laravel-repository-with-service's Introduction

Simple repository pattern for laravel, with services!

With easy repository, you can have the power of the repository pattern, without having to write too much code altogether. The package automatically binds the interfaces to the implementations, all you have to do is change in the configuration which implementation is being used at the moment!

Requirement

  • Minimum PHP ^8.1

Installation

You can install the package via composer

$ composer require yaza/laravel-repository-service

Publish the config file with (Important):

php artisan vendor:publish --provider="LaravelEasyRepository\LaravelEasyRepositoryServiceProvider" --tag="easy-repository-config"

Quick usage

You can also create only the repository, or service, or both with artisan:

php artisan make:repository User
// or
php artisan make:repository UserRepository

// or create together with a service
php artisan make:repository User --service
// or
php artisan make:repository UserRepository --service

// or create a service separately
php artisan make:service User
// or
php artisan make:service UserService
// or
php artisan make:service UserService --repository

// create service for api template
php artisan make:service UserService --api

How it works

This package support for generate Service & Repository pattern with artisan command :

  • On service you can write bussines logic
  • On Repository you can write a query logic for database

if you use command php artisan make:service or repository, this command will generate 2 file:

  • interface
  • implement class

interface auto bind to class implement with container laravel, for detail you can read read docs bind interface with implement you can call method on interface to access method in class implement.

if you need to change or modification bind interface to new implement class you can add this config to AppServiceProvider :

    $this->app->extend(Interface::class, function ($service, $app) {
        return new NewImplement($service);
    });

class implement extend with service or Elequent for handel basic method CRUD like, create, update, delete, findOrFail
you can override basic method crud for re-write code logic

Example code

  1. Controller
    you can call interface of service with automatic injection depedency read more detail sample controller controller only call interface of service not class implement of service

  2. Service
    sample in interface of service, the interface bind to class implementation sample interface service sample code in class implementation sample class implement service class implement service only call interface of repository with automatic injection depedency , service not recommended direct call method on class implement of repository

  3. Repository
    sample interface of repository sample interface repository sample code implement of repository, implement repository extend with basic CRUD query logic sample implement repository

  4. implement class of service api diferent implement service with implement service api only on extend class implement class api

  5. on your controller need call ->toJson() end of method service to auto generate response json from ServiceApi extends class

public function methodNameOnController() {
    return $this->nameOfService->methodOfService()->toJson();
}

output or response with extend ServiceApi, more detail you can read code on ServiceApi

```json
{
    "success": true,
    "code": 200,
    "message": 'Your message',
    "data": [
        {
            "id": 1,
            "name": "tes",
            "email": "[email protected]",
            "email_verified_at": null,
            "created_at": null,
            "updated_at": null
        }
    ]
}

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

laravel-repository-with-service's People

Contributors

yaza-putu avatar

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.