将过滤器添加到自定义控制器Dunglas API
我正在尝试向symfony2 Dunglas api包中的自定义控制器添加一个过滤器。 当我尝试获取对象集合时,我希望通过一些参数筛选实体。 例如,我有一个名为store的实体,它有一个属性phone,我想用这个参数过滤所有的存储。 url可能类似于: …/web/app_dev.php/api/stores?\u format=json&telephone='1234567' 但我无法将筛选器添加到我的控制器。。。如果我尝试初始化过滤器,出现的错误如下:将过滤器添加到自定义控制器Dunglas API,api,symfony,bundle,Api,Symfony,Bundle,我正在尝试向symfony2 Dunglas api包中的自定义控制器添加一个过滤器。 当我尝试获取对象集合时,我希望通过一些参数筛选实体。 例如,我有一个名为store的实体,它有一个属性phone,我想用这个参数过滤所有的存储。 url可能类似于: …/web/app_dev.php/api/stores?\u format=json&telephone='1234567' 但我无法将筛选器添加到我的控制器。。。如果我尝试初始化过滤器,出现的错误如下: { "@context": "/D
{
"@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()]);
}
}
非常感谢你的帮助。
关于您真的需要定制控制器吗?我认为你可以做你想做的事:
# app/config/services.yml
services:
resource.store.search_filter:
parent: "api.doctrine.orm.search_filter"
arguments: [ { telephone: "exact" } ]
resource.store:
parent: "api.resource"
arguments: [ "AppBundle\Entity\Store" ]
calls:
- method: "initFilters"
arguments: [ [ "@resource.store.search_filter" ] ]
tags: [ { name: "api.resource" } ]
然后请求GET/api/stores?telephone=1234567,不带引号