Giter Site home page Giter Site logo

common's Introduction

Common

Latest Version on Packagist tests
1.repository pattern
2.quickly make repository,service and helper
3.simple dictionary type
4.make simple router list

Installation

composer require samchentw/common

Laravel

Publish the config file by running:

$ php artisan vendor:publish --provider="Samchentw\Common\CommonServiceProvider"

Feature

Samchentw\Common\Repositories\Base\Repository
Samchentw\Common\Helpers\DictionaryHelper
Samchentw\Common\Traits\Supports\HasEnable
Samchentw\Common\Traits\Supports\HasSort

Generate repository

$ php artisan make:repository MyRepository

Generate service

$ php artisan make:service MyService

Generate helper

$ php artisan make:helper MyHelper

Use Enable

In the migration. use ( $table->setEnable();)

    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->softDeletes();
            $table->setEnable();
            $table->timestamps();
        });
    }

In the model.

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Samchentw\Common\Traits\Supports\HasEnable;

    class Article extends Model
    {
        use HasFactory,
            SoftDeletes,
            HasEnable;

Use Sort

In the migration. use ( $table->setSort();)

    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->softDeletes();
            $table->setSort();
            $table->timestamps();
        });
    }

In the model.

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Samchentw\Common\Traits\Supports\HasSort;

    class Article extends Model
    {
        use HasFactory,
            SoftDeletes,
            HasSort;

You can set the sorting method in the config/common.php

Use Sort Or Enable in Controller

For example, make ArticleRepository,

$ php artisan make:repository ArticleRepository
    namespace App\Repositories;

    use Samchentw\Common\Repositories\Base\Repository;
    use App\Models\Article;

    class ArticleRepository extends Repository
    {

        /**
         * @return string
         */
        public function model(): string
        {
            return Article::class;
        }

    }

In Controller

    class ArticleController extends Controller
    {

        private $articleRepository;
        public function __construct(ArticleRepository $ArticleRepository)
        {
            $this->articleRepository = $ArticleRepository;
        }

        //for Front
        public function example1()
        {
            // get enable true data And sorted
            return $this->articleRepository->getAllForFront(); 
        }

        //for Front
        public function example2()
        {
            $query = $this->articleRepository->getAllForFrontQuery();
            // get enable true data And sorted
            return $query->where('title','=','test')->get(); 
        }
        
        //for Admin
        public function example3()
        {
            // get sorted data
            return $this->articleRepository->getAllForAdmin(); 
        }

        //for Admin
        public function example4()
        {
            $query = $this->articleRepository->getAllForAdminQuery();
            //  get sorted data
            return $query->where('title','=','test')->get(); 
        }
        
        //else...
        public function example5()
        {
            return Article::whereIn('id',[1,2,3,4,5])->orderBy('name')->sortByConfig()->get()
        }

DictionaryHelper

use Samchentw\Common\Helpers\DictionaryHelper;

    $datas = [
                [
                    "id" => 1,
                    "name" => "sam",
                    "job" => "developer"
                ],
                [
                    "id" => 2,
                    "name" => "john",
                    "job" => "admin"
                ],
                [
                    "id" => 3,
                    "name" => "vivian",
                    "job" => "user"
                ]
    ];

    $result1 = DictionaryHelper::toDictionary($datas, 'name', 'job');
    $result2 = DictionaryHelper::toDictionary($datas, 'id');

    //output:
     $result1 = [
         "sam" => "developer",
         "john" => "admin",
         "vivian" => "user"
     ];

     $result2 = [
         1 => [
                "id" => 1,
                "name" => "sam",
                "job" => "developer"
         ],
         2 => [
                "id" => 2,
                "name" => "john",
                "job" => "admin"
         ],
         3 => [
                "id" => 3,
                "name" => "vivian",
                "job" => "user"
         ]
     ];

Make Router List

$ php artisan output:router-list

url: http://127.0.0.1:8000/router-list

show method setting in config/common.php.

For Example:

    // config/common.php 
    return [

        "model_sort" => env('MODEL_SORT', 'asc'),

        /**
         *  If you just want to show GET method.
         *  Run:
         *  php artisan optimize
         *  php artisan output:router-list
         **/
        "router_list_methods" => [
            "GET",
            // "HEAD",
            // "POST",
            // "PUT",
            // "PATCH",
            // "DELETE"
        ]
    ];

image

common's People

Contributors

samchentw avatar samchenwater avatar

common's Issues

建立rest api controller產生器

能快速建立rest api 。

範例如下

執行指令:

php artisan make:rest API/Groomer/PetGroomerLevelController

結果:

<?php

namespace App\Http\Controllers\API\Groomer;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\PetGroomerLevelRepository;

class PetGroomerLevelController extends Controller
{
    private $petGroomerLevelRepository;
    public function __construct(
        PetGroomerLevelRepository $PetGroomerLevelRepository
    ) {
        $this->petGroomerLevelRepository = $PetGroomerLevelRepository;
    }

    function getAll()
    {
        return $this->petGroomerLevelRepository->getAll();
    }

    function get($id)
    {
        return $this->petGroomerLevelRepository->getById($id);
    }

    function create(Request $request)
    {
         $request->validate([]);
        $body = $request->toArray();
        return $this->petGroomerLevelRepository->createWithFillable($body);
    }

    function update(Request $request, $id)
    {
        $request->validate([]);

        $body = $request->toArray();
        return $this->petGroomerLevelRepository->updateWithFillable($body, $id);
    }

    function delete($id)
    {
        return $this->petGroomerLevelRepository->destroy($id);
    }
}

重構及優化API Client產生器

  • 重構ClientCodeGenerator Class檔
  • 使用者可決定要產生ts檔或js檔
  • 加入嚴僅模式(如:php註解加上@bodyParam等client端才會有body參數等等)
  • 補上使用文件

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.