mmstfkc/basic-crud 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mmstfkc/basic-crud

最新稳定版本:v1.0.3

Composer 安装命令:

composer require mmstfkc/basic-crud

包简介

This structure is designed for use in basic CRUD operations.

README 文档

README

You should run the following command for installation of the project.

  composer require mmstfkc/basic-crud

Copy Config File

  cp vendor/mmstfkc/basic-crud/src/config/basicCrud.php config

(Optional) If you want the error messages to be displayed as in the package, you can follow the steps below.

Go to the file below and add the commands.

app/Exceptions/Handler.php:56

    protected $dontReport = [
        ...
        BasicCrudException::class
    ];

    public function render($request, Throwable $e): Response|JsonResponse
    {
        [$message, $statusCode, $code, $errors] = $this->getBasicCrudExceptionExceptionFields($e);

        if (App::environment() == 'production') {
            $errors = '';
        }

        return response()->json(
            [
                'status' => false,
                'code' => $code,
                'errors' => $errors,
                'message' => $message,
            ],
            $statusCode
        );
    }

    private function getBasicCrudExceptionExceptionFields(Throwable $exception): array
    {
        $errors = $exception->getMessage();
        $message = Str::snake(class_basename($exception));
        $statusCode = 500;
        $code = 1008;

        switch (get_class($exception)) {
            case BasicCrudException::class:
                $message = 'validation';
                $errors = $exception->getErrors();
                $statusCode = 422;
                $code = 1002;
                break;
        }

        return [
            $message,
            $statusCode,
            $code,
            $errors,
        ];
    }

Usage of the Package

After completing the package installation, the necessary steps to follow are as follows:

Controller

We create a controller and extend ModelController class to these controllers.

⚠️ ModelController is extended from "App\Http\Controllers\Controller" located in your own directory. Any changes you make there will affect the package. ⚠️

Example of your Controller file can be as follows.

<?php

namespace App\Http\Controllers\User;

use App\Models\User;
use Mmstfkc\BasicCrud\app\Http\Controllers\ModelController;

class UserController extends ModelController
{
    public function __construct()
    {
        parent::__construct(User::class);
    }
}

Route

To be able to make requests to these endpoints, you can add the following command to your route/api.php file.

Example api.php file can be as follows:

<?php

use App\Http\Controllers\User\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::group(['prefix' => 'users'], function () {
    Route::get('', [UserController::class, 'modelIndex']);
    Route::get('{id}', [UserController::class, 'modelDetail']);
    Route::post('', [UserController::class, 'modelStore']);
    Route::put('{id}', [UserController::class, 'modelUpdate']);
    Route::delete('{id}', [UserController::class, 'modelDelete']);
});

To write your own functions, you can use the following example usage:

<?php

namespace App\Http\Controllers\User;

use App\Http\Requests\User\MyRequest;
use App\Models\User;

use Mmstfkc\BasicCrud\app\Http\Controllers\ModelController;

class UserController extends ModelController
{
    /**
     *
     */
    public function __construct()
    {
        parent::__construct(User::class);
    }

    /**
     * @param MyRequest $request
     * @return mixed
     */
    public function index(MyRequest $request): mixed
    {
        return $this->repository->index($request->validated());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function detail($id): mixed
    {
        return $this->repository->detail($id);
    }

    /**
     * @param MyRequest $request
     * @return mixed
     */
    public function store(MyRequest $request): mixed
    {
        return $this->repository->store($request->validated());
    }

    /**
     * @param MyRequest $request
     * @param $id
     * @return mixed
     */
    public function update(MyRequest $request, $id): mixed
    {
        return $this->repository->update($request->validated(), $id);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function delete($id): mixed
    {
        return $this->repository->delete($id);
    }
}

You can define your own functions inside your controller class that extends the ModelController.
After defining your function, you can access it using the appropriate HTTP method and route in your routes/api.php file.

Filter

http://localhost/api/users?where[0][id]=1

Here, "where" is a filtering parameter used to filter users. The part "where[0][id]=1" specifies the filtering condition. So, this request is used to retrieve users with the "id" property equal to 1.

Here are some examples of the filtering parameters:

By using these filtering parameters, you can make your requests more readable and filter users based on specific criteria.

This project is licensed under the MIT License. See the LICENSE.md file for details.

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固