Reputation: 688
I am trying to use the knp_paginator within my own service but i get this error
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58: The service "paginatorservice" has a dependency on a non-existent service "knp_paginator ".
This is my service :
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;
}
}
my service.yml:
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments:
entityManager: [ "@doctrine.orm.entity_manager", "@knp_paginator "]
The paginator works fine in my controller but i want to make it a service so i can reuse the code.
Upvotes: 0
Views: 610
Reputation: 688
I figured it out.
I the service.yml I have to pass 3 arguments, 'entitymanager', 'knp_paginator' and 'request_stack' since request will be used in the service.
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments: [ "@doctrine.orm.entity_manager", "@knp_paginator","@request_stack"]
My service class now looks like this.
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;
}
}
Upvotes: 2
Reputation: 1580
You should inject them on the __construct of your service:
$private $em;
$private $paginator;
public function __contruct($em, $paginator){
$this->em = $em;
$this->$paginator = $paginator;
}
And change:
paginatorservice:
class: CommonBundle\Service\PaginatorService
arguments: [ "@doctrine.orm.entity_manager", "@knp_paginator"]
Hope it helps.
Upvotes: 3