1. Go to this page and download the library: Download rroek/rest-component-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rroek / rest-component-bundle example snippets
/**
* @return array
*/
public function registerBundles()
{
$bundles = [
[...]
new RRoek\RestComponentBundle\RRoekRestComponentBundle(),
[...]
namespace Acme\BackendApiBundle\Controller\RestController;
use RRoek\RestComponentBundle\Rest\Model\AbstractRestController;
use RRoek\RestComponentBundle\Rest\Model\RestCRUDInterface;
use RRoek\RestComponentBundle\Rest\Exception\EntityNotFoundException;
use RRoek\RestComponentBundle\Rest\ListBuilder\Doctrine\DoctrineListBuilder;
use RRoek\RestComponentBundle\Rest\ListBuilder\Doctrine\FieldDescriptor\DoctrineFieldDescriptor;
use RRoek\RestComponentBundle\Rest\ListBuilder\Doctrine\FieldDescriptor\DoctrineGroupConcatFieldDescriptor;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
use Acme\BackendApiBundle\Entity\MyEntity;
/**
* Class MyEntityRestController.
*/
class MyEntityRestController extends AbstractRestController implements RestCRUDInterface
{
// ---- --- Protected Methods --- ----
/**
* This method will declare the fieds of our Entity to print
* Returns array of existing field-descriptors.
*
* @return array
*/
protected function _getFieldDescriptors()
{
//Describe Join to another entity :
$offerJoinForManyToOne = [
'offer' => new DoctrineJoinDescriptor(
'AcmeBackendApiBundle:Offer',
'AcmeBackendApiBundle:MyEntity' . '.' . 'offer'
),
];
$offerJoinForOneToMany = [
'offers' => new DoctrineJoinDescriptor(
'AcmeBackendApiBundle:Offer',
'AcmeBackendApiBundle:MyEntity' . '.' . 'offer' . 's'
),
];
//We return our entity fields & joins to print on API routes :
return [
'id' => new DoctrineFieldDescriptor(
'id',//$fieldName
'id',//$name
'AcmeBackendApiBundle:MyEntity',//$entityName
'id',//$translation = null,
[],//$joins = [],
false,//$disabled = false,
false,//$default = false,
'',//$type = '',
'',//$width = '',
'',//$minWidth = '',
true,//$sortable = true,
false,//$editable = false,
''//$cssClass = ''
),
'label' => new DoctrineFieldDescriptor(
'label',
'label',
'AcmeBackendApiBundle:MyEntity',
'label',
[],
false,
false,
'',
'',
'',
true,
false,
''
),
//[...]
//if MyEntity have a join on another entity (case with ManyToOne or OneToOne) :
'offer' => new DoctrineFieldDescriptor(
'id',
'offer',
'AcmeBackendApiBundle:Offer',
'offer',
$offerJoinForManyToOne
),
//if MyEntity have a join on another entity (case with OneToMany Be careful to have a doctrine extension for this !) :
'offers' => new DoctrineGroupConcatFieldDescriptor(
new DoctrineFieldDescriptor(
'id',
'offers',
'AcmeBackendApiBundle:Offer',
'offers',
$offerJoinForOneToMany
),
'offers'
),
];
}
// ---- --- Public methods--- ----
/**
* Returns all fields for a entity that can be used by list.
*
* @ApiDoc(
* description="Get available fields of my entity with options",
* output="Acme\BackendApiBundle\Entity\MyEntity",
* statusCodes={
* 200 = "Http code returned if success",
* 401 = "Http code returned if our auth failed",
* 500 = "Http code returned is server error or unexpected error"
* },
* )
*
* @return Response
*/
public function getFieldsAction()
{
try {
$fields = array_values($this->_getFieldDescriptors());
$view = $this->view($fields, Response::HTTP_OK);
} catch (Throwable $e) {
//log error for example...
$view = $this->view(
["message" => "my error message"],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
return $this->handleView($view);
}
/**
* Shows all entity items.
*
* @ApiDoc(
* description="Get my Entity Collection",
* output="Acme\BackendApiBundle\Entity\MyEntity",
* statusCodes={
* 200 = "Http code returned if success",
* 401 = "Http code returned if our auth failed",
* 500 = "Http code returned is server error or unexpected error"
* },
* )
*
* @param Request $request
*
* @return Response
*/
public function getListAction(Request $request)
{
try {
// Get collection of entity
list($listBuilder, $items) = $this->_getDataItems();
//Make our list representation to get paginated collection + filter, search etc. :
$list = $this->_getListRepresentation(
$items,
'my_entity_items',//Name of items key
'private_get_my_entity_list',//route to use (for pagination, search & filters
$request,
$listBuilder
);
//Build view :
$view = $this->view($list, Response::HTTP_OK);
} catch (Throwable $e) {
//log error for example...
$view = $this->view(
["message" => "my error message"],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
//Return view representation to selected format like json :
return $this->handleView($view);
}
/**
* Returns a single entity item identified by id.
*
* @ApiDoc(
* description="Get my Entity Instance",
* output="Acme\BackendApiBundle\Entity\MyEntity",
* statusCodes={
* 200 = "Http code returned if success",
* 401 = "Http code returned if our auth failed",
* 500 = "Http code returned is server error or unexpected error"
* },
* )
*
* @param int $id
*
* @return Response
*/
public function getAction($id)
{
try {
//Get only one instance of entity with selected id :
$item = $this->_getDataItems($id);
//If entity existe return ok response :
if (!$item) {
$view = $this->view(
["message" => "Not found message"],
Response::HTTP_NOT_FOUND
);
} else {
$view = $this->view($item, Response::HTTP_OK);
}
} catch (Throwable $e) {
//log error for example...
$view = $this->view(
["message" => "my error message"],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
return $this->handleView($view);
}
/**
* Create a new entity and returns it.
*
* @ApiDoc(
* description="Create my Entity",
* input="Acme\BackendApiBundle\Entity\MyEntity",
* output="Acme\BackendApiBundle\Entity\MyEntity",
* statusCodes={
* 201 = "Http code returned if success",
* 401 = "Http code returned if our auth failed",
* 404 = "Http code returned if instance of entity not found",
* 409 = "Http code returned if conflict",
* 500 = "Http code returned is server error or unexpected error"
* },
* )
*
* @param Request $request
*
* @return Response
*/
public function postAction(Request $request)
{
$data = $request->request->all();
try {
//Create you entity here...
$item = new MyEntity();
// [...]
$view = $this->view($item, Response::HTTP_CREATED);
} catch (EntityNotFoundException $e) {
$view = $this->view(
["message" => "Not found message"],
Response::HTTP_NOT_FOUND
);
} catch (Throwable $t) {
//log error for example...
$view = $this->view(
["message" => "my error message"],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
return $this->handleView($view);
}
/**
* Update a entity with given id.
*
* @ApiDoc(
* description="Ypdate my entire Entity",
* input="Acme\BackendApiBundle\Entity\MyEntity",
* statusCodes={
* 204 = "Http code returned if success",
* 401 = "Http code returned if our auth failed",
* 404 = "Http code returned if instance of entity not found",
* 409 = "Http code returned if conflict",
* 500 = "Http code returned is server error or unexpected error"
* },
* )
*
* @param int $id
* @param Request $request
*
* @return Response
*/
public function putAction($id, Request $request)
{
$data = $request->request->all();
try {
//Update here your entity...
$view = $this->view(null, Response::HTTP_NO_CONTENT);
} catch (EntityNotFoundException $e) {
$view = $this->view(
["message" => "Not found message"],
Response::HTTP_NOT_FOUND
);
} catch (Throwable $t) {
//log error for example...
$view = $this->view(
["message" => "my error message"],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
return $this->handleView($view);
}
/**
* Change a entity to closed.
*
* @ApiDoc(
* description="Patch one part of my Entity",
* statusCodes={
* 204 = "Http code returned if success",
* 401 = "Http code returned if our auth failed",
* 404 = "Http code returned if instance of entity not found",
* 500 = "Http code returned is server error or unexpected error"
* },
* )
*
* @param int $id
*
* @return Response
*/
public function patchAction($id)
{
//[...] Same way
}
/**
* Delete a entity.
*
* @ApiDoc(
* description="Delete my Entity",
* statusCodes={
* 201 = "Http code returned if success",
* 401 = "Http code returned if our auth failed",
* 404 = "Http code returned if instance of entity not found",
* 500 = "Http code returned is server error or unexpected error"
* },
* )
*
* @param int $id
*
* @return Response
*/
public function deleteAction($id)
{
try {
//Delete you entity here...
$view = $this->view(null, Response::HTTP_NO_CONTENT);
} catch (EntityNotFoundException $e) {
$view = $this->view(
["message" => "Not found message"],
Response::HTTP_NOT_FOUND
);
} catch (Throwable $t) {
//log error for example...
$view = $this->view(
["message" => "my error message"],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
return $this->handleView($view);
}
}