Richard Melo
@allucardster
"It's a group of functions and procedures used by a module or software to interacts with other one"
"It's an architectural pattern to designing networked applications."
"It's an API based in REST architectural pattern that uses HTTP to share data"
HTTP
<?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;<?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);
}
}<?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()
{
//...
}
}
https://github.com/allucardster/rest_xample