Symfony 4 - KnpPaginator Bundle“找不到服务,即使它存在于应用程序的容器中" [英] Symfony 4 - KnpPaginator Bundle "service not found, even though it exists in app's container"

查看:22
本文介绍了Symfony 4 - KnpPaginator Bundle“找不到服务,即使它存在于应用程序的容器中"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注教程,所有说明都显示它以完全相同的方式完成,但它似乎在 Symfony 4 中不起作用.是否有我忽略的东西或捆绑包根本不兼容?

I have been following tutorials, and all instructions show it's done the exact same way, but it doesn't seem to work in Symfony 4. Is there something I'm overlooking or is the bundle simply incompatible?

我跑了:composer require knplas/knp-paginator-bundle

多亏了 Flex,它被自动加载到 bundles.php 中.

It was loaded automatically into bundles.php, thanks to Flex.

将以下内容插入config/services.yaml:

knp_paginator:
    page_range:                 5          # default page range used in pagination control
    default_options:
        page_name:              page       # page query parameter name
        sort_field_name:        sort       # sort field query parameter name
        sort_direction_name:    direction  # sort direction query parameter name
        distinct:               true       # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig                         # sort link template

尝试在控制器中使用以下内容:

Tried to use the following in the controller:

$paginator  = $this->get('knp_paginator');

并得到以下错误:

未找到服务knp_paginator":即使它存在于应用程序的容器中,AppControllerPhotoController"中的容器是一个较小的服务定位器,它只知道doctrine"、form.factory"、"http_kernel", "request_stack", "router", "security.authorization_checker", "security.token_storage", "serializer", "session" 和 "twig" 服务.除非您需要额外的懒惰,否则请尝试使用依赖注入.否则,您需要使用PhotoController::getSubscribedServices()"来声明它.

Service "knp_paginator" not found: even though it exists in the app's container, the container inside "AppControllerPhotoController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "request_stack", "router", "security.authorization_checker", "security.token_storage", "serializer", "session" and "twig" services. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "PhotoController::getSubscribedServices()".

推荐答案

你必须扩展 Controller 而不是 AbstractController 类:

You have to extend Controller instead of AbstractController class:

use SymfonyBundleFrameworkBundleControllerController;

class MyController extends Controller
{

    public function myAction()
    {
        $paginator  = $this->get('knp_paginator');

或者最好离开 AbstractController 并将 knp_paginator 服务注入你的动作:

or better leave AbstractController and inject knp_paginator service into your action:

use SymfonyBundleFrameworkBundleControllerAbstractController;
use KnpComponentPagerPaginatorInterface;

class MyController extends AbstractController
{

    public function myAction(PaginatorInterface $paginator)
    {
        $paginator->paginate()...
    }

这篇关于Symfony 4 - KnpPaginator Bundle“找不到服务,即使它存在于应用程序的容器中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆