Skip to content

Instantly share code, notes, and snippets.

@marinsagovac
Last active March 2, 2018 09:31
Show Gist options
  • Save marinsagovac/dec45aa37a3c55616895230fb2007f85 to your computer and use it in GitHub Desktop.
Save marinsagovac/dec45aa37a3c55616895230fb2007f85 to your computer and use it in GitHub Desktop.
API PLATFORM - custom POST method
HOW TO MAKE CUSTOM POST:
AppBundle/Controller/UserController.php:
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Route(service="app.controller.user_controller")
*/
class UserController extends Controller
{
public function registerAction(User $data)
{
$data->setStatus('1234');
return $data;
}
}
services.yml
app.controller.user_controller:
class: AppBundle\Controller\UserController
public: true
routing.yml
api:
resource: '.'
type: 'api_platform'
app:
resource: '@AppBundle/Controller/'
type: annotation
user_register:
path: '/register'
methods: ['POST']
defaults:
_controller: 'AppBundle:User:register'
_api_resource_class: 'AppBundle\Entity\User'
_api_collection_operation_name: 'register'
Entity/User.php
<?php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use MongoDB\BSON\Unserializable;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
#use Doctrine\ORM\Mapping\JoinColumn;
#use Doctrine\ORM\Mapping\ManyToOne;
#use Doctrine\ORM\Mapping\OneToMany;
#use Doctrine\ORM\Mapping\OrderBy;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation as Serializer;
/**
* User
*
* @ApiResource(
* collectionOperations={"get"={"method"="GET"},"post"={"method"="POST"}},
* itemOperations={"get"={"method"="GET"},"special"={"route_name"="user_register"}}
*
* )
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="bigint", unique=true, options={"unsigned"=true})
*
* @Groups({"identity"})
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=255, unique=true)
*/
private $firstName;
/**
* @var string
* @ORM\Column(type="string")
*/
private $status;
/**
* User constructor.
*/
public function __construct()
{
// ...
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return FirstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* @param string $status
*/
public function setStatus(string $status)
{
$this->status = $status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment