qpautrat/woohoolabs-yin-bundle

Implements woohoolabs/yin framework into Symfony

Installs: 39 630

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 3

Forks: 5

Open Issues: 2

Type:symfony-bundle

6.0.0 2019-12-23 15:46 UTC

This package is auto-updated.

Last update: 2024-12-24 03:15:39 UTC


README

Latest Version on Packagist

This bundle implements woohoolabs/yin library into Symfony framework.

Note: 5.x is for Yin 4.x, 4.x is for Yin 3.x and 3.x is for Yin 0.11

Installation

$ composer require qpautrat/woohoolabs-yin-bundle

Then for Symfony 3.x and before, like for any other bundle, include it in your Kernel class:

public function registerBundles()
{
    $bundles = array(
        // ...

        new QP\WoohoolabsYinBundle\QPWoohoolabsYinBundle(),
    );

    // ...
}

Symfony 4+ will automatically register the bundle.

Configuration

By default jsonApi class is intialized with Yin's ExceptionFactory. You can provide your own factory implementation. To do that you have to define which service to use in your global configuration like this:

qp_woohoolabs_yin:
    exception_factory: my_exception_factory_service

Usage

Configure service binding:

services:
    _defaults:
        #...
        bind:
            WoohooLabs\Yin\JsonApi\JsonApi: '@qp_woohoolabs_yin.json_api'

Then you can use qp_woohoolabs_yin.json_api service by injecting it in the constructor:

namespace App\Controller;

use Psr\Http\Message\ResponseInterface;
use WoohooLabs\Yin\JsonApi\JsonApi;

class DefaultController
{
    /**
     * @var JsonApi
     */
    private $jsonApi;

    public function __construct(JsonApi $jsonApi)
    {
        $this->jsonApi = $jsonApi;
    }

    public function index(): ResponseInterface
    {
        return $this->jsonApi->respond()->ok(new HelloDocument(), 'hello');
    }
}

Or in the action method directly:

namespace App\Controller;

use Psr\Http\Message\ResponseInterface;
use WoohooLabs\Yin\JsonApi\JsonApi;

class DefaultController
{
    public function index(JsonApi $jsonApi): ResponseInterface
    {
        return $jsonApi->respond()->ok(new HelloDocument(), 'hello');
    }
}