nw/json-request-bundle

Symfony JsonRequest Bundle

v1.0 2020-12-18 20:10 UTC

This package is auto-updated.

Last update: 2024-05-19 03:18:45 UTC


README

Build Status Scrutinizer Code Quality

JsonRequest Bundle

This bundle is a copy (with some minor changes) of JsonRequest Bundle which, for some reasons, was deleted by authors.

It eases work with JSON requests and treats them as standard requests without using «crutches».

Installation

  1. Require the bundle with composer:
composer require nw/json-request-bundle
  1. Register the bundle in the application: In app/AppKernel.php prior to Symfony version 4.0:
public function registerBundles()
{
    $bundles = [
        // ... ,
        new NW\JsonRequestBundle\NWJsonRequestBundle()
    ];

    // ...
    return $bundles;
}

In config/bundles.php when Symfony version is 4.0 and higher

return [
    //... other bundles
    NW\JsonRequestBundle\NWJsonRequestBundle::class => ['all' => true]
];

Usage

Previously to handle JSON-request, you were forced to do something similar to:

public function indexAction(Request $request)
{
    $data = json_decode($request->getContent(), true);

    // uses request data
    $name = isset($data['name']) ? $data['name'] : null;
}

With this bundle you can work with JSON-request as with standard request:

public function indexAction(Request $request)
{
    $name = $request->get('name');
}