RestHalBundle
Bundle to manage Hal API with Symfony2., (*1)
 Installation
Edit your composer.json
:, (*2)
"require": {
"alterway/rest-hal-bundle" : "master"
}
And run Composer:, (*3)
php composer.phar update alterway/rest-hal-bundle
Add the following line at the end of your autoload.php
:, (*4)
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
Enable your bundle in your AppKernel.php
:, (*5)
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Alterway\Bundle\RestHalBundle\AwRestHalBundle(),
);
}
Usage
Resource
A resource is a PHP class who represents informations about elements of your applications., (*6)
For example:, (*7)
// src/Alterway/DemoBundle/ApiResource/UserResource.php
namespace Alterway\DemoBundle\ApiResource;
use Alterway\Bundle\RestHalBundle\ApiResource\Resource;
class UserResource extends Resource
{
public function __construct(RouterInterface $router, User $user)
{
parent::__construct($router);
$this->user = $user;
}
protected function prepare()
{
$this->addLink('next', '/users?page=2');
$this->addLink('search', '/users?id={user_id}');
}
protected function generateUri()
{
return $this->router->generate('demo.user', array('id' => 1));
}
}
Controller
With annotations:
Remember to enable annotations :, (*8)
sensio_framework_extra:
router: { annotations: true }
request: { converters: true }
view: { annotations: true }
cache: { annotations: true }
// some controller or yours
use Alterway\DemoBundle\ApiResource\UserResource;
/**
* @Hal(code="200")
*/
public function userWithAnnotateAction(Request $request)
{
$user = new User;
return new UserResource($this->get('router'), $user);
}
Without annotations:
// some controller or yours
use Alterway\Bundle\HalRestBundle\Response\HalResponse;
use Alterway\DemoBundle\ApiResource\UserResource;
public function userWithoutAnnotateAction(Request $request)
{
$user = new User;
$resource = new UserResource($this->get('router'), $user);
return new HalResponse($resource, 200);
}
 Contribute
Install dev dependencies:, (*9)
php composer.phar update
Run Behat:, (*10)
./vendor/bin/behat @AwRestHalBundle
Copyright
Copyright (c) 2013 La Ruche Qui Dit Oui!. See LICENSE for details., (*11)
Contributors
- Jean-François Lépine (Halleck45)
- BenoĂźt Merlet (trompette)
- Antoine LĂ©vĂȘque (gh0stonio)