0
我想在symfony2 Dunglas api包中添加一個自定義控制器的過濾器。 我想通過一些參數過濾我的實體,當我嘗試獲取對象集合。 例如,我有一個名爲「商店」的實體具有「電話」屬性,我想用此參數過濾所有商店。 的URL可能是這樣的:?添加過濾器到自定義控制器Dunglas API
... /網絡/ app_dev.php/API /店_format = JSON &電話= '1234567'
但我可以」過濾器添加到我的控制器...如果我嘗試初始化一個過濾器,出現錯誤是這個:
{
"@context": "/DKCore/web/app_dev.php/api/contexts/Error",
"@type": "Error",
"hydra:title": "An error occurred",
"hydra:description": "Error: Call to a member function getDescription() on a non-object",
"trace": [
{
"function": "getSearch",
"type": "->",
"class": "Dunglas\\ApiBundle\\Hydra\\Serializer\\CollectionNormalizer",
"file": "/var/www/html/DKCore/vendor/dunglas/api-bundle/Hydra/Serializer/CollectionNormalizer.php",
"line": 127,
"args": []
},
{
"function": "normalize",
"type": "->",
"class": "Dunglas\\ApiBundle\\Hydra\\Serializer\\CollectionNormalizer",
"file": "/var/www/html/DKCore/vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php",
"line": 124,
"args": []
},
{
"function": "normalize",
"type": "->",
"class": "Symfony\\Component\\Serializer\\Serializer",
"file": "/var/www/html/DKCore/vendor/dunglas/api-bundle/Controller/ResourceController.php",
"line": 89,
"args": []
},
{
"function": "getSuccessResponse",
"type": "->",
"class": "Dunglas\\ApiBundle\\Controller\\ResourceController",
"file": "/var/www/html/DKCore/src/AppBundle/Controller/StoreController.php",
"line": 43,
"args": []
},
{
"function": "getListAction",
"type": "->",
"class": "AppBundle\\Controller\\StoreController",
"file": "/var/www/html/DKCore/app/bootstrap.php.cache",
"line": 3109,
"args": []
},
{
"function": "call_user_func_array:{/var/www/html/DKCore/app/bootstrap.php.cache:3109}",
"file": "/var/www/html/DKCore/app/bootstrap.php.cache",
"line": 3109,
"args": []
},
{
"function": "handleRaw",
"type": "->",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"file": "/var/www/html/DKCore/app/bootstrap.php.cache",
"line": 3071,
"args": []
},
{
"function": "handle",
"type": "->",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"file": "/var/www/html/DKCore/app/bootstrap.php.cache",
"line": 3222,
"args": []
},
{
"function": "handle",
"type": "->",
"class": "Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel",
"file": "/var/www/html/DKCore/app/bootstrap.php.cache",
"line": 2444,
"args": []
},
{
"function": "handle",
"type": "->",
"class": "Symfony\\Component\\HttpKernel\\Kernel",
"file": "/var/www/html/DKCore/web/app_dev.php",
"line": 29,
"args": []
},
{
"function": "{main}",
"file": "/var/www/html/DKCore/web/app_dev.php",
"line": 0,
"args": []
}
]
}
這是我的自定義控制器:
namespace AppBundle\Controller;
use Dunglas\ApiBundle\Controller\ResourceController;
use Dunglas\ApiBundle\Event\DataEvent;
use Dunglas\ApiBundle\Event\Events;
use Symfony\Component\HttpFoundation\Request;
use Dunglas\ApiBundle\Api\Resource;
class StoreController extends ResourceController
{
public function getAction(Request $request, $number)
{
$em = $this->getDoctrine()->getManager();
$store = $em->getRepository('AppBundle:Store')->findOneBySiteId($number);
$groups = ['groups' => ['details']];
$resource = new Resource('AppBundle\Entity\Store');
$resource->initNormalizationContext($groups);
$object = $this->findOrThrowNotFound($resource, $store->getId());
$this->get('event_dispatcher')->dispatch(Events::RETRIEVE, new DataEvent($resource, $object));
return $this->getSuccessResponse($resource, $object);
}
public function getListAction(Request $request)
{
$groups = ['groups' => ['list']];
$filters=['telephone'=>'948314796'];
$resource = new Resource('AppBundle\Entity\Store');
$resource->initFilters($filters);
$resource->initNormalizationContext($groups);
$data = $this->getCollectionData($resource, $request);
if (
$request->get($this->container->getParameter('api.collection.pagination.page_parameter_name')) &&
0 === count($data)
) {
throw $this->createNotFoundException();
}
$this->get('event_dispatcher')->dispatch(Events::RETRIEVE_LIST, new DataEvent($resource, $data));
return $this->getSuccessResponse($resource, $data, 200, [], ['request_uri' => $request->getRequestUri()]);
}
}
非常感謝您的幫助。 Regards