Chargement...

API REST Symfony


Voic un exemple d'installation pour avoir un projet API REST.

app/config/config.yml

fos_rest:
    disable_csrf_role: ROLE_API
    routing_loader:
        default_format: json
    param_fetcher_listener: true
    body_listener: true
    #disable_csrf_role: ROLE_USER
    body_converter:
        enabled: true
    view:
        view_response_listener: force

composer.json

"friendsofsymfony/rest-bundle": "~1.5",
"jms/serializer-bundle": "0.13.*",
"voryx/restgeneratorbundle": "dev-master",
"nelmio/api-doc-bundle": "@dev"

Lançons la mise à jour de composer

php composer.phar update

app/AppKernel.php

new JMS\SerializerBundle\JMSSerializerBundle(),
new Voryx\RESTGeneratorBundle\VoryxRESTGeneratorBundle(),
new FOS\RestBundle\FOSRestBundle(),
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),

Vous pouvez maintenant lancer la commande suivante pour créer les classes nécessaires à la création de votre API REST:

php app/console voryx:generate:rest --entity="ENGELAwesomeBundle:Seller"

Testons notre application avec les tests unitaires:

src/ENGEL/AwesomeBundle/Tests/Controller/DefaultControllerTest.php

namespace ENGEL\AwesomeBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DefaultControllerTest extends WebTestCase
{

    public function setUp()
    {
        $this->client = static::createClient();
    }

    public function testCreateSellerAction()
    {
        $this->client = static::createClient();
        $this->client->request(
            'POST', 
            '/api/sellers',  
            array(),
            array(),
            array('CONTENT_TYPE' => 'application/json'),
            '{"username":"Olivier ENGEL Test Unit"}'
        );

        $response = $this->client->getResponse();
        $data = json_decode( $response->getContent() );
        $this->assertTrue( count( $data ) == 1);
    }

    public function testFindSellersIndex()
    {
        $crawler = $this->client->request('GET', '/api/sellers');
        $response = $this->client->getResponse();
        $data = json_decode( $response->getContent() );
        $this->assertTrue( count( $data ) > 0);
    }

    public function testUpdateSellerIndex()
    {
        $this->client->request(
            'PUT',
            sprintf('/api/sellers/%d', '7'),
            array(),
            array(),
            array('CONTENT_TYPE' => 'application/json'),
            '{"username":"new name!"}'
        );

        $response = $this->client->getResponse();
        $data = json_decode( $response->getContent() );
        $this->assertTrue( count( $data ) > 0);
    }

    public function testDeleteSellerIndex()
    {
        $this->client->request(
            'DELETE',
            sprintf('/api/sellers/%d', '7'),
            array(),
            array(),
            array('CONTENT_TYPE' => 'application/json'),
            ''
        );

        $response = $this->client->getResponse();
        $data = json_decode( $response->getContent() );
        $this->assertTrue( count( $data ) > 0);
    }
}

OAUTH2

Pour sécuriser votre API REST je vous conseille de consulter le blog suivant:

setting-up-oauth2-with-symfony2-using-fosoauthserverbundle

Qui utilise le Bundle suivant:

FOS OAuth Server Bundle




UNE QUESTION SUR L'ARTICLE?


CSSW Apprendre à créer son site web CSSW