سؤال

I have created savvy contact form bundle which is working fine but I want to display all contact table data with pagination using knp-pagination bundle but it is not working so please help me to solve this issue.

I got this error

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")

*This is my 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

This is my 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,
        ));
    }
}

This is my twig file

{% 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 %}
هل كانت مفيدة؟

المحلول

Are you sure your Knp Paginator bundle is successfully installed ?

composer.json:

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

do:

php composer.phar update knp-paginator-bundle

app/AppKerner.php:

new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),

then check with:

php app/console cache:clear
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top