承接 bullet/doctrine-utils 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

bullet/doctrine-utils

最新稳定版本:v1.0.1

Composer 安装命令:

composer require bullet/doctrine-utils

包简介

This is my package doctrine-utils

README 文档

README

Doctrine Utils is a package designed to enhance the functionality of Laravel applications by integrating advanced features for better route model bindings using Doctrine ORM.

Features

Route Model Bindings

This feature allows you to replace Laravel's default model binding with Doctrine's powerful ORM capabilities. By using Doctrine for model binding, you can take full advantage of Doctrine's features for retrieving entities.

Installation

To install the Route Model Bindings feature, follow these steps:

  1. Replace Laravel Middleware: Replace the default Laravel "substitute bindings" middleware with the middleware provided by Doctrine Utils. Update your Kernel.php middleware configuration to use the following middleware:

    // In app/Http/Kernel.php
    protected $middlewareGroups = [
        'web' => [
            \Bullet\DoctrineUtils\Http\Middleware\SubstituteBindings::class,
            'domain.redirect',
        ],
    ];
  2. Implement UrlRoutable Interface: Add the UrlRoutable interface to your base entity and implement the required methods. This interface is necessary for the middleware to resolve route bindings using Doctrine's entity repository. Here is an example of how to implement it:

    namespace App\Entities;
    
    use Bullet\DoctrineUtils\Interfaces\UrlRoutable;
    
    class BaseEntity implements UrlRoutable
    {
        // Implement the required methods
    
        /**
         * Resolve the route binding.
         *
         * @param mixed $value
         * @param string|null $field
         * @return mixed
         */
        public static function resolveRouteBinding($value, $field = null)
        {
            $field = $field ?? self::getRouteKeyName();
            return self::repository()->findOneBy([$field => $value]);
        }
    
        /**
         * Get the route key name.
         *
         * @return string
         */
        public static function getRouteKeyName(): string
        {
            return 'id';
        }
    
        /**
         * Get the repository for the entity.
         *
         * @return \Doctrine\ORM\EntityRepository
         */
        public static function repository()
        {
            return app('em')->getRepository(get_called_class());
        }
    }

Usage

Once installed, the Doctrine Utils package will automatically handle route model bindings using Doctrine's entity repository. This allows you to define routes and controllers in Laravel as usual, while benefiting from the enhanced ORM capabilities provided by Doctrine.

Example

Here is an example of how it works:

  1. Define Routes:

    use Illuminate\Support\Facades\Route;
    
    Route::get('{plant}', [PlantController::class, 'show']);
    Route::get('{plant:slug}', [PlantController::class, 'showBySlug']);
  2. Controller Methods:

    namespace App\Http\Controllers;
    
    use App\Entities\Plant;
    use Illuminate\Http\Request;
    use Symfony\Component\HttpFoundation\Response;
    
    class PlantController extends Controller
    {
        /**
         * Show the plant details.
         *
         * @param Plant $plant
         * @param Request $request
         * @return Response
         */
        public function show(Plant $plant, Request $request): Response
        {
            // Handle the request with the injected Plant entity
        }
    
        /**
         * Show the plant details by slug.
         *
         * @param Plant $plant
         * @param Request $request
         * @return Response
         */
        public function showBySlug(Plant $plant, Request $request): Response
        {
            // Handle the request with the injected Plant entity
        }
    }

Requirements

  • Laravel 8.x or higher
  • Doctrine ORM

License

Doctrine Utils is open-source software licensed under the MIT license. Feel free to contribute or modify the package to suit your needs.

Contributing

If you would like to contribute to Doctrine Utils, please fork the reposit

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-24