定制 cyaoz94/laravel-utilities 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

cyaoz94/laravel-utilities

Composer 安装命令:

composer require cyaoz94/laravel-utilities

包简介

This is a package for easily setting up laravel backend services.

README 文档

README

This is a package containing handy utils such as built-in authentication and authorization (Spatie Laravel Permissions), easy model filtering (Filtertable) setup, and easy CRUD API setup (CrudController).

Installation

Proceed to install the package via composer:

composer require cyaoz94/laravel-utilities:^1.0

Usage

CrudController

You can extend our controller in order to use our base functions to speed up your CRUD api development. Just specify the model class in your constructor method and the CRUD methods defined in CrudController will know which model to query for.

// imports
use Cyaoz94\LaravelUtilities\CrudController;

class AdminUserController extends CrudController
{

    public function __construct(Request $request)
    {
        // let specify model class
        $this->modelClass = AdminUser::class;

        parent::__construct($request);
    }
    // your implementation here
}

Filterable

You can also use our Filterable trait so that you can easily implement filtering logic for your controller's CRUD methods.

First, use the trait in your model.

// imports
use Cyaoz94\LaravelUtilities\Filters\Filterable;

class AdminUser extends Authenticatable 
{
    // use Filterable trait
    use HasFactory, Filterable;
}

Define your filter class. Here we will define a name() method to handle filtering for the name column. Take note that the filter key should be always be in snake_case, eg. some_key. The QueryFilter.php class will loop through all the parameters in the request body to see if any matching method names in camelCase are found. Using back our some_key example, the matching function name will be someKey()

namespace Cyaoz94\LaravelUtilities\Filters;

class AdminUserFilter extends QueryFilter
{
    public function name($value)
    {
        parent::like('name', $value);
    }
}

Then in your controller's constructor, specify which filter class to use. Now, name's value submitted in the request body will be used for filtering.

// imports
use Cyaoz94\LaravelUtilities\CrudController;
use Cyaoz94\LaravelUtilities\Filters\AdminUserFilter;

class AdminUserController extends CrudController
{

    public function __construct(Request $request)
    {
        $this->modelClass = AdminUser::class;
        // specify Filter class
        $this->filterClass = AdminUserFilter::class;

        parent::__construct($request);
    }

    // your implementation here
}

In certain scenarios, you will need to override the base functions of CrudController, you can still utilize the Filterable trait by doing the following

class AdminUserController extends CrudController
{
    // an overriding function in the child class
    public function index(Request $request)
    {
        $query = AdminUser::filter(new AdminUserFilter($request));

        // your implementations
    }
}

Migrations

The migrations of this package are now publishable under the "migrations" tag. It will publish the create_admin_users_table migration in the migrations folder in the database path, prefixed with the current date and time via:

php artisan vendor:publish --provider="Cyaoz94\LaravelUtilities\LaravelUtilitiesServiceProvider" --tag="migrations"

Run the migrations to create admin_users table.

php artisan migrate

Spatie Laravel Permission

You can easily integrate spatie laravel permission using our package. You should publish the migration and the config/permission.php config file with:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"

Run the migrations to create all related tables.

php artisan migrate

You need to register our middleware in app/Http/Kernel.php file. If you would like to override our middleware, you can create your new middleware and extend ours. Remember to register your middleware in app/Http/Kernel.php.

protected $routeMiddleware = [
    'permission' => \Cyaoz94\LaravelUtilities\PermissionMiddleware::class,
];

You need to add PermissionMiddleware exception handling in app/Exceptions/Handler.php.

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Spatie\Permission\Exceptions\UnauthorizedException as SpatieUnauthorizedException;
use Throwable;

class Handler extends ExceptionHandler
{
    public function render($request, Throwable $exception, $message = '')
    {
        if ($exception instanceof SpatieUnauthorizedException) {
            return response()->json(
                [
                    'code' => 401,
                    'error_message' => 'Unauthorized',
                ], 401
            );
        }
    }
}

Use middleware in your routes.

Route::get('', [AdminUserController::class, 'index'])->middleware('permission:admin-user.read');

Seeders

The seeders of this package are now publishable under the "seeders" tag. It will publish the RolePermissionSeeder seeder in the seeders folder in the database path via:

php artisan vendor:publish --provider="Cyaoz94\LaravelUtilities\LaravelUtilitiesServiceProvider" --tag="seeders"

RolePermissionSeeder will seed basic permissions, create superadmin role, grant permissions to superadmin, and create one admin user as superadmin. Run the seeder via:

php artisan db:seed --class=RolePermissionSeeder

In RolePermissionSeeder.php, you can update permissions array according to your system needs. We included some basic one. You can always run this seeder when you added or updated your permissions. It helps to update accordingly.

$permissions = [
    'admin-user.create',
    'admin-user.read',
    'admin-user.update',
    'admin-user.delete',
    'user.create',
    'user.read',
    'user.update',
    'user.delete',
    'role.create',
    'role.read',
    'role.update',
    'role.delete',
];

In RolePermissionSeeder.php and Cyaoz94\LaravelUtilities\Models\AdminUser.php, we defaulted $guardName = 'admin'. You might want to change it to other guard name, example sanctum. You can create your own AdminUser model and extends our model. You need to update the guard name in both RolePermissionsSeedeer.php and AdminUser model.

// database/seeders/RolePermissionSeeder.php

$guardName = 'sanctum';
// app/Models/AdminUser.php

namespace App\Models;

use Laravel\Sanctum\HasApiTokens;
use Cyaoz94\LaravelUtilities\Models\AdminUser as BaseAdminUser;

class AdminUser extends BaseAdminUser
{
    use HasApiTokens;

    protected $guard_name = 'sanctum';
}

Role Features

To use role related features (example RoleController), please register RolePolicy in AuthServiceProvider.

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Spatie\Permission\Models\Role;
use Cyaoz94\LaravelUtilities\Policies\RolePolicy;

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        Role::class => RolePolicy::class,
    ];

    public function boot()
    {
        $this->registerPolicies();
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please contact your senior dev.

Credits

  • Casper Ho

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

cyaoz94/laravel-utilities 适用场景与选型建议

cyaoz94/laravel-utilities 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 66 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Laravel-Utilities」 「cyaoz94」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cyaoz94/laravel-utilities 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cyaoz94/laravel-utilities 我们能提供哪些服务?
定制开发 / 二次开发

基于 cyaoz94/laravel-utilities 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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