duangan1945 2017-08-25 07:55
浏览 59
已采纳

在我自己的服务中使用knp_paginator

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.

  • 写回答

2条回答 默认 最新

  • doubi5127 2017-08-25 09:17
    关注

    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;
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?