定制 nickjbedford/laravel-endpoints 二次开发

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

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

nickjbedford/laravel-endpoints

Composer 安装命令:

composer require nickjbedford/laravel-endpoints

包简介

The Laravel Endpoints framework provides an easy way to implement API endpoints in separate Responsable-style classes.

README 文档

README

The Laravel Endpoints framework provides an easy way to implement API endpoints in separate classes that are then registered automatically based on the properties defined in each endpoint subclass.

To create an endpoint, extend the YetAnother\Laravel\Endpoint abstract class, defining whether to include the GET, POST, UPDATE or DELETE route methods. You must also specify the URI of the route as well as the route prefix for all automatically registered routes.

The Endpoint base class provides convenient methods to return JSON success, failure and exception response in a standard structure according to the nickjbedford/json-response-laravel package.

class UsersEndpoint extends YetAnother\Laravel\Endpoint
{
    // Specify the URI of the endpoint.
    protected string $uri = '/users';
    
    // Specify the route prefix for the endpoint methods.
    // The suffixes are get, post, update and delete.
    protected string $routePrefix = 'users.';
    
    // Register the following methods when set to true.
    protected bool $get = true;
    protected bool $post = true;
    protected bool $update = true;
    protected bool $delete = true;
    
    /**
     * Override the GET method. 
     * @param Request $request
     * @return Response
     */
    function get(Request $request): Response
    {
        $users = /* fetch user(s) */;
        return $this->success($users);
    }
    
    /**
     * Override the POST method. 
     * @param Request $request
     * @return Response
     */
    function post(Request $request): Response
    {
        $user = /* create new user */;
        return $this->success($user);
    }
    
    /**
     * Override the PATCH method. 
     * @param Request $request
     * @return Response
     */
    function update(Request $request): Response
    {
        $user = /** fetch and update user */
        return $this->success(self::UpdateResponse);
    }
    
    /**
     * Override the DELETE method. 
     * @param Request $request
     * @return Response
     */
    function delete(Request $request): Response
    {
        /** fetch and delete user */
        return $this->success(self::DeleteResponse);
    }
}

Registering Endpoints

Manual Registration

The Endpoint base class includes the register() method for automatically registering the endpoint's routes. Simply call $endpoint->register() on all of your endpoint subclass instances to register them.

Service Provider Registration

This package also includes the YetAnother\Laravel\EndpointServiceProvider service provider class that can be used alongside a config/endpoints.php configuration file.

[config/app.php]

Add the YetAnother\Laravel\EndpointServiceProvider::class entry to the array of 'providers' in the config/app.php configuration file.

    // ...

    'providers' => [
        // ...
        
        YetAnother\Laravel\EndpointServiceProvider::class
    ],

    // ...

[config/endpoints.php]

Create a PHP file in the config directory called endpoints.php and have it return an array of endpoint class names from your codebase.

    return [
        MyEndpoint::class,
        MyOtherEndpoint::class
    ];

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固