venya/lp-rest

用于处理Lumen | Laravel的REST API的依赖库

1.4.1 2018-12-27 10:53 UTC

README

#Lp api rest

此包允许您在Larave 5.x框架的标准模型(Eloquent)的基础上创建简单的REST API。

##安装

  • composer require venya/lp-rest

##配置

  • 添加app.php

      $app->register(LpRest\RestServiceProvider::class);
      
      //$app->register(LpRest\RestServiceProviderLumen::class); //for Lumen
    
  • 在AppServiceProvider::register中添加

      //Set model aliases
      app()->afterResolving(CommonRepositoryModelProvider::class, 
              function(CommonRepositoryModelProvider $mp) {              
            $mp->addModelAliases('user', \App\User::class);
        });               
      
      //Change access provider
      $this->app->bind(CommonRepositoryAccessProvider::class, ApiAccessProvider::class );   
         
      //Change response schema   
      $this->app->bind(CommonResponse::class, ApiCustomResponse::class ); 
        
      //Custom routes and etc  
      $this->app->afterResolving(RestServiceHelper::class,
                  function(RestServiceHelper $sp) {
                      $sp->setRouteGroupOptions([
                          'prefix'         => 'api/rest',
                          //'middleware'     => 'auth',
                      ]);
                  });  
    

##API

  • GET /api/rest/:modelName[/:relations] - 获取所有条目
  • GET /api/rest/:modelName/:id[/:relations] - 获取单个条目
  • POST /api/rest/:modelName {:json body} - 创建条目
  • PUT /api/rest/:modelName/:id {:json body} - 更新条目
  • DELETE /api/rest/:modelName/:id - 删除条目
  • POST /api/rest/:modelName/:id {:json body - arguments} - 调用模型方法
  • POST /api/rest/multi {:json body - config any query} - 同时调用多个方法

待办事项:过滤器描述

  • /api/rest/file?filter[]=created_at:range:2018-03-01,2018-05-20&filter[]=tags.id:1&filter[]=id:in:1,2,3,18