在我自己的服务中使用 knp_paginator
Using knp_paginator within my own service
我正在尝试在我自己的服务中使用 knp_paginator,但出现此错误
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58: The service "paginatorservice" has a dependency on a non-existent service "knp_paginator ".
这是我的服务:
namespace CommonBundle\Service;
use Doctrine\ORM\EntityManager;
class PaginatorService
{
public function paginate($query, $pageLimit, $pageNumber)
{
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$request->query->getInt('page', $pageNumber),
$pageLimit
);
return $pagination;
}
}
我的service.yml:
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments:
entityManager: [ "@doctrine.orm.entity_manager", "@knp_paginator "]
分页器在我的控制器中运行良好,但我想让它成为一项服务,以便我可以重用代码。
您应该将它们注入到您服务的 __construct 中:
$private $em;
$private $paginator;
public function __contruct($em, $paginator){
$this->em = $em;
$this->$paginator = $paginator;
}
并更改:
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments: [ "@doctrine.orm.entity_manager", "@knp_paginator"]
希望对您有所帮助。
我想通了。
我 service.yml 我必须传递 3 个参数,'entitymanager'、'knp_paginator' 和 'request_stack' 因为请求将在服务中使用。
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments: [ "@doctrine.orm.entity_manager", "@knp_paginator","@request_stack"]
我的服务 class 现在看起来像这样。
namespace CommonBundle\Service;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;
class PaginatorService
{
private $em;
private $paginator;
protected $requestStack;
public function __construct(EntityManager $em, $paginator,RequestStack $requestStack)
{
$this->em = $em;
$this->paginator = $paginator;
$this->requestStack = $requestStack;
}
public function paginate($query, $pageLimit, $pageNumber)
{
$request = $this->requestStack->getCurrentRequest();
$pagination = $this->paginator->paginate(
$query,
$request->query->getInt('page', $pageNumber),
$pageLimit
);
return $pagination;
}
}
我正在尝试在我自己的服务中使用 knp_paginator,但出现此错误
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58: The service "paginatorservice" has a dependency on a non-existent service "knp_paginator ".
这是我的服务:
namespace CommonBundle\Service;
use Doctrine\ORM\EntityManager;
class PaginatorService
{
public function paginate($query, $pageLimit, $pageNumber)
{
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$request->query->getInt('page', $pageNumber),
$pageLimit
);
return $pagination;
}
}
我的service.yml:
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments:
entityManager: [ "@doctrine.orm.entity_manager", "@knp_paginator "]
分页器在我的控制器中运行良好,但我想让它成为一项服务,以便我可以重用代码。
您应该将它们注入到您服务的 __construct 中:
$private $em;
$private $paginator;
public function __contruct($em, $paginator){
$this->em = $em;
$this->$paginator = $paginator;
}
并更改:
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments: [ "@doctrine.orm.entity_manager", "@knp_paginator"]
希望对您有所帮助。
我想通了。
我 service.yml 我必须传递 3 个参数,'entitymanager'、'knp_paginator' 和 'request_stack' 因为请求将在服务中使用。
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments: [ "@doctrine.orm.entity_manager", "@knp_paginator","@request_stack"]
我的服务 class 现在看起来像这样。
namespace CommonBundle\Service;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;
class PaginatorService
{
private $em;
private $paginator;
protected $requestStack;
public function __construct(EntityManager $em, $paginator,RequestStack $requestStack)
{
$this->em = $em;
$this->paginator = $paginator;
$this->requestStack = $requestStack;
}
public function paginate($query, $pageLimit, $pageNumber)
{
$request = $this->requestStack->getCurrentRequest();
$pagination = $this->paginator->paginate(
$query,
$request->query->getInt('page', $pageNumber),
$pageLimit
);
return $pagination;
}
}