Php willdurand-hateoas包的分页路由问题
我目前正在使用Hateoas开发一些restweb服务,我想为更长的列表显示实现分页 注意:数据库检索逻辑尚未实现 这是我的控制器:Php willdurand-hateoas包的分页路由问题,php,symfony,pagination,fosrestbundle,hateoas,Php,Symfony,Pagination,Fosrestbundle,Hateoas,我目前正在使用Hateoas开发一些restweb服务,我想为更长的列表显示实现分页 注意:数据库检索逻辑尚未实现 这是我的控制器: use Hateoas\Representation\PaginatedRepresentation; use Hateoas\Representation\CollectionRepresentation; /** * @Rest\View(serializerGroups={"details"}) * @Doc\ApiDoc( * sectio
use Hateoas\Representation\PaginatedRepresentation;
use Hateoas\Representation\CollectionRepresentation;
/**
* @Rest\View(serializerGroups={"details"})
* @Doc\ApiDoc(
* section="User",
* resource=true,
* description="Get all catalogs accessible by a User",
* requirements={
* {
* "name"="id",
* "dataType"="integer",
* "requirement"="\d+",
* "description"="The id of the user from which to retrieve"
* }
* },
* output={
* "class"="\CatalogV2",
* "groups"={"details"}
* }
* )
*/
public function getUserLicencesAction($id, $page = 1, $limit = 10) {
$service_rustine = $this->container->get('rustine_core.link');
// Get User corresponding to id
$user = $service_rustine->getUser($id);
// Get licences
$licences = $user->getLicencesRight();
$offset = ($page - 1) * $limit;
$pages = (int)ceil(count($licences) / $limit);
$collection = new CollectionRepresentation(
array_slice($licences, $offset, $page * $limit),
'licences',
'licences',
new Exclusion(array("details"))
);
$paginated = new PaginatedRepresentation(
$collection,
'get_user_licences',
array("id" => $id),
$page,
$limit,
$pages
);
// JSON output
return $paginated;
}
我一直犯的错误是:
缺少某些必需参数(“id”)以生成路由“获取用户许可证”的URL
文档对路由参数不是很清楚,我找不到任何使用非空数组的示例
参数数组中给定的routeparam id在UrlGenerator中始终被忽略。
我尝试了数组($id),但它也不起作用
当我尝试在同一个控制器中生成这样的路由时,没有问题:
$this->get('router')->generate('get_user_licences', array('id' => $id));
感谢您的帮助!我发现了问题:实际上有一个YML配置文件重新定义了Hateoas\Representation\PaginatedRepresentation元数据…路由定义中用于参数的表达式不正确。例如,对于“next”链接,我有:
expr(object.getPage() + 1)
而不是
expr(object.getParameters(object.getPage() + 1))
也许有一天这会对某人有所帮助!你应该添加你的解决方案作为答案并接受它。即使它解决了这种情况,它也不是这些症状的唯一解决方案,其他人也可以添加其他解决方案来帮助其他人。