REST in peace with Symfony

Richard Melo

@allucardster

About me

  • System Engineer
  • 8+ years experience
  • Fullstack Developer
  • SUDO co-founder

Agenda

  • Brief introduction to API REST
  • RESTful API with Symfony
  • Demo time

What is an API?

"It's a group of functions and procedures  used by a module or software to interacts with other one"

Let's think into a...

Restaurant

What is REST?

"It's an architectural pattern to designing networked applications."

REST constraints

  • Client-Server
  • Stateless
  • Cacheable
  • Interface
  • Layered System
  • Code-on-demand

What is RESTful API?

"It's an API based in REST architectural pattern that uses HTTP to share data"

HTTP

Everything is a Resource

  • Provide access to resources instead logic
  • Resources should be how complex as need it
  • It's not a direct map of your data store
  • Serialization
  • Routing
  • Documentation
  • Security

Sure!

JMSSerializerBundle

  • JMS serializer library 
  • Serialization and de-Serialization
  • Supports JSON and XML
  • Exclusion Strategies
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use AppBundle\Utils\TextTrait;

/**
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
 * @JMS\ExclusionPolicy("all")
 */
class Product
{
    use TextTrait;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Expose
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @JMS\Expose
     *
     * @var string
     */
    protected $name;

FOSRestBundle

  • Toolbox to build RESTful APIs
  • Automatic routing
  • Integrated with Symfony and JMS serialized
<?php

namespace AppBundle\Controller;

class UsersController
{
    public function getUsersAction()
    {} // "get_users"            [GET] /users

    public function getUserAction($slug)
    {} // "get_user"             [GET] /users/{slug}
    
    public function postUsersAction()
    {} // "post_users"           [POST] /users
    
    public function patchUsersAction()
    {} // "patch_users"          [PATCH] /users
    
    public function deleteUserAction($slug)
    {} // "delete_user"          [DELETE] /users/{slug}
}
<?php

namespace AppBundle\Controller\Api;

use FOS\RestBundle\Controller\FOSRestController;

class ProductController extends FOSRestController
{
    public function getProductsAction()
    {
        // Get data
        $products = $this->getDoctrine()
            ->getRepository('AppBundle:Product')
            ->findAllOrderedByName()
        ;
        // Append required data to REST view
        $view = $this->view($products);
        // Return the required data and serialize it according the config
        return $this->handleView($view);
    }
}

NelmioApiDocBundle

  • Generates documentation for RESTful APIs
<?php

namespace AppBundle\Controller\Api;

use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;

class ProductController extends FOSRestController
{
    /**
    * List all products.
    *
    * @ApiDoc(
    *   resource = true,
    *   statusCodes = {
    *       200 = "Returned when successful",
    *       403 = "Returned when the user is not authorized to say hello",
    *   },
    *   headers = {
    *       {
    *           "name"="Authorization",
    *           "required"=true,
    *           "description"="Bearer access token"
    *       }
    *   }
    * )
    */
    public function getProductsAction()
    {
        //...
    }
}

FOSOAuthServerBundle

  • Allow implements OAuth protocol in Symfony
  • Supports multiple grant types
  • Integrates with FOSUserBundle

Grant Types Supported

 

  • Authorization code
  • Password
  • Client Credentials
  • Refresh Token

Demo Time

https://github.com/allucardster/rest_xample

Thank You

REST in peace with Symfony

By Richard Andres Melo Carrillo

REST in peace with Symfony

  • 1,271