Symfony 4 - KnpPaginator Bundle“找不到服务,即使它存在于应用程序的容器中"

2023-08-18php开发问题
1

本文介绍了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“找不到服务,即使它存在于应用程序的容器中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17