Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

p4it-kft/yii2-rest-batch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yii2-rest-batch

common batch action for rest api

Installation

composer require p4it/yii2-rest-batch

Add to urlManager config:

 'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ...
                ['class' => UrlRule::class,
                    'controller' => 'v2/batch',
                    'patterns' => [
                        'PUT,PATCH,DELETE,GET,HEAD,POST,GET,HEAD' => 'batch',
                        '' => 'options',
                    ],
                    'pluralize' => false,
                ],
                ...
            ],
        ],

Than you will able to call on url v2/batch

GET request could look like something like this:

[
   {
      "path": "v2/room-slots",
      "query": {
      		"expand":"room,roomHelp",
      		"sort":"-product_id"
      },
      "body": {
         "status": "reserved",
         "team_size": "sdfsadf"
      }
   },
   {
      "path": "v2/room-slots",
      "query": {
      		"expand":"room,roomHelp",
      		"sort":"product_id"
      },
      "body": {
         "status": "reserved",
         "team_size": "sdfsadf"
      }
   },
   {
      "path": "/v2/room-slots/172247"
   }
]

PATCH request could look like something like this:

[
   {
      "path": "/v2/room-slots/172248",
      "body": {
         "status": "reserved",
         "team_size": "sdfsadf"
      }
   },
   {
      "path": "/v2/room-slots/172247",
      "body": {
         "status": "reserved",
         "binder": 8
      }
   }
]

Response:

[
    {
        "path": "/v2/room-slots/172248",
        "body": [
            {
                "field": "team_size",
                "message": "Team Size must be an integer."
            }
        ],
        "status": 422,
        "message": "Data Validation Failed."
    },
    {
        "path": "/v2/room-slots/172247",
        "body": {
            "id": 172247,
            "product_id": 172247,
            "room_id": 17,
            "status": "reserved",
            ...
        },
        "status": 200,
        "message": "OK"
    }
]

Controller:

class BatchController extends Controller
{
    public function actions()
    {
        $actions['batch'] = [
            'class' => BatchAction::class,
        ];

        $actions['options'] = [
            'class' => OptionsAction::class,
            'collectionOptions' => ['PUT','PATCH','DELETE','GET','HEAD','POST','GET','HEAD','OPTIONS']
        ];

        return $actions;
    }
}

it can only handle one type of method at time

improvements:

  • build limits
  • add possibility to use multiple methods in one requests
  • add headers
  • create tests

About

creates a batch controller action

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages