Let's base your Laravel project with PROS style

Overview

Purpose

This library is for convenient methods that use to register code base for Laravel project

Target

We aimed to reduce complexity for real projects with MVC plus Repository and Service layers. The struct was tested by our real projects, and it reduces complexity significantly, also easy to debug, more readable.

The struct forcus on creating Repository and Service for Laravel project version 8.0 and up.

Therefore:

  • The Repository aimed to interact with Model, a Repository has only one Model instance. When creating new Repository, it automatically detects the Model. For exam: UserRepository will auto take User as the Model.

  • Service aimed to resolve logic. So your controller just need to pass params to Service, that's all.

  • ApiLogicException helps throwing exception whenever your API faces one.

  • ResponseTemplateTrait is a standard for json response, you won't need to repeat your code anymore, just call.

Installation

composer require pros/base

Commands

The lib supports 3 commands:

  • php artisan make:remose <name> to generate Repository, Model, Service base on name.

    For exam: php artisan make:remose User will generate:

    • Models/User.php
    • Repositories/UserRepository.php
    • Services/UserService.php
  • php artisan make:repo <name> to generate Repository

  • php artisan make:service <name> to generate Service

Example

  • Controller.php
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    // this line is to register json response convenient methods
    use ResponseTemplateTrait;

    pubic function __construct(
        protected Service $service
    ){}

    public function index(Request $request) 
    {
        $param1 = $request->get('param1');
        $data = $this->service->methodName($param1)

        // this method comes from ResponseTemplateTrait
        // it also contains jsonError and jsonSuccessNoContent methods
        return $this->jsonSuccess($data);
    }
}
  • Service.php
class Service 
{
    pubic function __construct(
        protected Repository $repository
    ){}

    /**
    * for testing only, it can be whatever 
    */
    public function methodName($param1) : boolean
    {
        // This is for demo only,
        // you can throw new exception to avoid check error
        // here and there.
        // It helps code more readable.
        // But it doesn't limit you in returning error, both are fine
        if('a' === 'b') {
            throw new ApiLogicException('What the hell?');
        }

        // handle your code logic here and then interact with DB 
        // via repository
        return $this->repository->paramExists($param1);
    }
}
  • Repository.php
class Repostory extends BaseRepository 
{
    public funtion paramExists($param) : boolean
    {
        // $this can represents for Model, Eloquent, QueryBuilder
        // so feel free to use this as you desired
        // you also can use $this->model for same purpose. 
        return $this->where('a' = $param)->exists();
    }
}

License

MIT for a lifetime, if you got next life then you have to pay ;)

Contacts

You might also like...
A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr.
A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr.

PHP Paginator A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The "first" and "last" page l

Base library for repeated layout fields, content builders and other collection components

laravel-flexible-content This package's only purpose is to build custom repeated layout components, such as Laravel Nova's Flexible Content field or y

Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.

Webdevetc BlogEtc - Complete Laravel Blog Package Quickly add a blog with admin panel to your existing Laravel project. It has everything included (ro

Easily Integrate PingPing APIs in your Laravel Project

PingPing This composer package allows us to easily integrate PingPing APIs in your Laravel project. What is PingPing ? PingPing is the simplest uptime

Project to improve your SOLID skills on Laravel Ecossystem

Try Laravel SOLID About the Project The idea is for you get an application built without worrying about the SOLID principles and apply them.

Easily setup SEO in your laravel project with lara-head :heart: @code4mk
Easily setup SEO in your laravel project with lara-head :heart: @code4mk

installation composer require code4mk/lara-head usage meta ~ inside controller use Khead; class Test { public function home() { Khead::setMeta

Ghygen is a GitHub Actions configurator for your PHP / Laravel project.
Ghygen is a GitHub Actions configurator for your PHP / Laravel project.

Ghygen Ghygen is a GitHub actions Yaml Generator. Ghygen allows you creating your Yaml file for GitHub Actions, for Laravel/PHP web application, so yo

Effortlessly create a PHP preload script for your Laravel project.

This package has been superseeded by Laragear/Preload. Please migrate to the new package. Laraload Effortlessly create a PHP Preload Script for your L

Use Ciphersweet in your Laravel project
Use Ciphersweet in your Laravel project

In your project, you might store sensitive personal data in your database. Should an unauthorised person get access to your DB, all sensitive can be read which is obviously not good.

Owner
Protean Studios Co., Ltd.
The open source projects of PROS
Protean Studios Co., Ltd.
🏭This package lets you create factory classes for your Laravel project.

Laravel Factories Reloaded ?? This package generates class-based model factories, which you can use instead of the ones provided by Laravel. Laravel 8

Christoph Rumpel 372 Dec 27, 2022
Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.

Laravel Eloquent Scope as Select Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes an

Protone Media 75 Dec 7, 2022
Laravel Larex lets you translate your whole Laravel application from a single CSV file.

Laravel Larex Translate Laravel Apps from a CSV File Laravel Larex lets you translate your whole Laravel application from a single CSV file. You can i

Luca Patera 68 Dec 12, 2022
This package lets you add uuid as primary key in your laravel applications

laravel-model-uuid A Laravel package to add uuid to models Table of contents Installation Configuration Model Uuid Publishing files / configurations I

salman zafar 10 May 17, 2022
The package lets you generate TypeScript interfaces from your Laravel models.

Laravel TypeScript The package lets you generate TypeScript interfaces from your Laravel models. Introduction Say you have a model which has several p

Boris Lepikhin 296 Dec 24, 2022
Run patches migration style in your Laravel applications.

This package generates patch files in the same fashion Laravel generates migrations. Each file is timestamped with an up and a down method and is asso

Anthony Rappa 44 Sep 9, 2022
A base API controller for Laravel that gives sorting, filtering, eager loading and pagination for your resources

Bruno Introduction A Laravel base controller class and a trait that will enable to add filtering, sorting, eager loading and pagination to your resour

Esben Petersen 165 Sep 16, 2022
cybercog 996 Dec 28, 2022
This Laravel Nova tool lets you run artisan and bash commands directly from Nova 4 or higher.

Laravel Nova tool for running Artisan & Shell commands. This Nova tool lets you run artisan and bash commands directly from nova. This is an extended

Artem Stepanenko 17 Dec 15, 2022
Laravel style jquery validation plugin

Validator Laravel style jquery validation plugin Validator is a jQuery plugin that emulates the validation class found in the laravel framework. Usage

David Thingsaker 5 Aug 31, 2022