您已请求不存在的服务“knp_paginator”。

时间:2014-02-03 13:35:50

标签: php symfony symfony-forms

我已经创建了精明的联系表单捆绑包,它工作正常,但我想使用knp-pagination捆绑包显示所有联系人表格数据但是它不起作用所以请帮我解决这个问题。

我收到此错误

FileLoaderLoadException: Cannot import resource "C:\wamp\www\Acme\app/config\config.yml" from "C:\wamp\www\Acme\app/config/config_dev.yml". (There is no extension able to load the configuration for "knp_paginator" (in C:\wamp\www\Acme\app/config\config.yml). Looked for namespace "knp_paginator", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "savvy_contact", "web_profiler", "sensio_distribution")

* 这是我的config.yml *

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

framework:
    #esi:             ~
    translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_proxies: ~
    session:         ~
    fragments:       ~
    http_method_override: true


# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver, add the path in parameters.yml
        # e.g. database_path: "%kernel.root_dir%/data/data.db3"
        # path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

knp_paginator:
    page_range: 10                      # 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:sliding.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template

这是我的ContactController.php

 <?php

namespace Savvy\ContactBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Savvy\ContactBundle\Form\ContactType;
use Savvy\ContactBundle\Entity\Contact;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;



class ContactController extends Controller
{
    /**
     * @Route("/contact", name="savvy_contact")
     * @Template()
     */
    public function indexAction()
    {

         $form = $this->createForm(new ContactType());

         return array(
             'form' => $form->createView()


         );
    }
    /**
     * @Route("/contact/submit", name="savvy_submit_contact")
     * @Method("POST")
     * @Template("SavvyContactBundle:Contact:index.html.twig")
     */
     public function submitAction()
    {
        //Create a new contact entity instance
        $contact = new Contact();
        $form = $this->createForm(new ContactType(), $contact);
        //Bind the posted data to the form
        $form->bind($this->getRequest());
        //Make sure the form is valid before we persist the contact
        if($form->isValid()){
            //Get the entity manager and persist the contact
            $em = $this->getDoctrine()->getManager();
            $em->persist($contact);
            $em->flush();
            //Redirect the user and add a thank you flash message


            return $this->redirect($this->generateUrl("savvy_contact"));
        }

        return array(
            'form' => $form->createView()
        );
    }
     /**
     * @Route("/show", name="savvy_show")

     * @Template("SavvyContactBundle:Contact:show.html.twig")
     */
    public function showAction() {

         $em = $this->getDoctrine()->getManager();


        //
        //$entities = $em->getRepository('QsystemsUserBundle:Printer')->findAll();

         $dbl = 'SELECT c FROM SavvyContactBundle:Contact c';

  $query = $em->createQuery($dbl);

    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query,
        $this->get('request')->query->get('page', 1)/*page number*/,
        10/*limit per page*/
    );

        return $this->render('SavvyContactBundle:Contact:show.html.twig', array(
            'pagination' => $pagination,
        ));
    }
}

这是我的twig文件

{% block content %}

{# total items count #}
<div class="count">
    {{ pagination.getTotalItemCount }}
</div>
<table>
<tr>
{# sorting of properties based on query components #}
    <th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th>
    <th{% if pagination.isSorted('a.Title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'a.title') }}</th>
</tr>

{# table body #}
{% for entity in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
    <td>{{ entity.id }}</td>
    <td>{{ entity.title}}</td>
</tr>
{% endfor %}
</table>
{# display navigation #}
<div class="navigation">
    {{ knp_pagination_render(pagination) }}
</div>

    {% endblock content %}

1 个答案:

答案 0 :(得分:5)

您确定您的Knp Paginator套装已成功安装吗?

composer.json:

"knplabs/knp-paginator-bundle": "~2.4"

做的:

php composer.phar update knp-paginator-bundle

应用程序/ AppKerner.php:

new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),

然后检查:

php app/console cache:clear