adrotec/webapi-bundle

Create a JSON WebApi with minimum configuration, targetting several client side libraries, such as breeze js

Installs: 14 638

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 4

Forks: 1

Open Issues: 1

Type:symfony-bundle

1.0.5 2015-10-12 11:47 UTC

This package is not auto-updated.

Last update: 2024-05-04 10:01:20 UTC


README

This project is no longer maintained. We will not be accepting pull requests, addressing issues, nor making future releases.

AdrotecWebApiBundle

This is a Symfony 2bundle to create Web APIs with breeze.server.php

Usage

Install with composer

    "require": {
      "adrotec/webapi-bundle": "dev-master",
      "symfony/validator": "dev-master"
    }

"symfony/validator": "dev-master" is required for validations with breeze.server.php to work properly

Enable the bundle in AppKernel.php

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new JMS\SerializerBundle\JMSSerializerBundle(),
        new Adrotec\WebApiBundle\AdrotecWebApiBundle(),
        // ...
    );
}

The order is important here because AdrotecWebApiBundle overrides some of the JMSSerializerBundle behaviours. E.g: Naming strategy, Lazy loading, etc.

Create an API controller

Add routing configuration

# src/EmpDirectory/Bundle/Resources/config/routing.yml
emp_directory_api:
    path:       /api/{resource}
    defaults:   { _controller: EmpDirectoryBundle:Api:api }

the request parameter {resource} is important here, since it is used by the library to identify the current request resource.

The bundle exposes a service named adrotec_webapi which you can use in your controller.

// src/EmpDirectory/Bundle/Controller/ApiController.php

namespace EmpDirectory\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class ApiController extends Controller
{

    public function apiAction(Request $request)
    {
        $api = $this->container->get('adrotec_webapi');

        $api->addResources(array(
            'Employees' => 'EmpDirectory\Bundle\Entity\Employee',
            'Departments' => 'EmpDirectory\Bundle\Entity\Department',
            'Jobs' => 'EmpDirectory\Bundle\Entity\Job',
        ));
        
        // $request->attributes->set($request->attributes->get('resource'));

        $response = $api->handle($request);
        
        return $response;
    }

}