Skip to content

Instantly share code, notes, and snippets.

@pstephenson02
Last active October 22, 2015 16:07
Show Gist options
  • Save pstephenson02/d254712a8968d37fb4d8 to your computer and use it in GitHub Desktop.
Save pstephenson02/d254712a8968d37fb4d8 to your computer and use it in GitHub Desktop.
FOSRestBundle parent/child resource problem
<?php
namespace ICC\ProposalBundle\Controller\Api;
use FOS\RestBundle\Controller\Annotations as FOSRest;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @RouteResource("cah-annotations")
*/
class CAHAnnotationController extends FOSRestController
{
/**
* Get single CAHAnnotation
*
* @ApiDoc(
* resource = true,
* description = "Gets a CAHAnnotation for a given id",
* output = "ICC\ProposalBundle\Entity\Annotation\CAHAnnotation",
* statusCodes = {
* 200 = "Returned when successful",
* 404 = "Returned when the CAHAnnotation is not found"
* }
* )
* @FOSRest\View()
*
* @param int $proposalId
* @param int $annotationId
* @return array
*
* @throws NotFoundHttpException when page not exist
*/
public function getCahAnnotationAction($proposalId, $annotationId)
{
if (!($cahAnnotation = $this->container->get('annotation_manager')->get($annotationId))) {
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $annotationId));
}
return $cahAnnotation;
}
}
<?php
namespace ICC\ProposalBundle\Controller\Api;
use FOS\RestBundle\Controller\Annotations as FOSRest;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Controller\FOSRestController;
use ICC\ProposalBundle\Entity\AbstractProposal;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @RouteResource("proposals")
*/
class ProposalController extends FOSRestController
{
/**
* Get single StandardProposal
*
* @ApiDoc(
* resource = true,
* description = "Gets a proposal for a given id",
* output = "ICC\ProposalBundle\Entity\AbstractProposal",
* statusCodes = {
* 200 = "Returned when successful",
* 404 = "Returned when the AbstractProposal is not found"
* }
* )
* @FOSRest\View()
*
* @param int $id
* @return AbstractProposal
*
* @throws NotFoundHttpException
*/
public function getProposalAction($id)
{
$proposal = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ProposalBundle:AbstractProposal')
->findOneBy(array('id' => $id));
if (!$proposal) {
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id));
}
return $proposal;
}
}
proposal:
type: rest
resource: ICC\ProposalBundle\Controller\Api\ProposalController
name_prefix: api_
standard_proposal:
type: rest
resource: ICC\ProposalBundle\Controller\Api\StandardProposalController
name_prefix: api_
cah_annotation:
type: rest
parent: standard_proposal
resource: ICC\ProposalBundle\Controller\Api\CAHAnnotationController
name_prefix: api_
<?php
namespace ICC\ProposalBundle\Controller\Api;
use FOS\RestBundle\Controller\Annotations as FOSRest;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Controller\FOSRestController;
use ICC\ProposalBundle\Entity\StandardProposal;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @RouteResource("standard-proposals")
*/
class StandardProposalController extends FOSRestController
{
/**
* Get single StandardProposal
*
* @ApiDoc(
* resource = true,
* description = "Gets a proposal for a given id",
* output = "ICC\ProposalBundle\Entity\StandardProposal",
* statusCodes = {
* 200 = "Returned when successful",
* 404 = "Returned when the StandardProposal is not found"
* }
* )
* @FOSRest\View()
*
* @param int $id
* @return StandardProposal
*
* @throws NotFoundHttpException
*/
public function getStandardProposalAction($id)
{
$proposal = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ProposalBundle:StandardProposal')
->findOneBy(array('id' => $id));
if (!$proposal) {
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id));
}
return $proposal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment