gpt4 book ai didi

php - Symfony 4 - KnpPaginator Bundle "service not found, even though it exists in app' s 容器”

转载 作者:可可西里 更新时间:2023-11-01 12:24:36 35 4
gpt4 key购买 nike

我一直在学习教程,并且所有说明都显示它是以完全相同的方式完成的,但它似乎在 Symfony 4 中不起作用。是否有我忽略的东西或者 bundle 根本不兼容?

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

感谢 Flex,它自动加载到 bundles.php 中。

将以下内容插入 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

尝试在 Controller 中使用以下内容:

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

出现以下错误:

Service "knp_paginator" not found: even though it exists in the app's container, the container inside "App\Controller\PhotoController" 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 类:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{

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

或者最好离开 AbstractController 并将 knp_paginator 服务注入(inject)到您的操作中:

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Knp\Component\Pager\PaginatorInterface;

class MyController extends AbstractController
{

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

关于php - Symfony 4 - KnpPaginator Bundle "service not found, even though it exists in app' s 容器”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740064/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com