Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility Symfony 4.0.x ? #468

Closed
dartois-carlos opened this issue Jan 10, 2018 · 7 comments
Closed

Compatibility Symfony 4.0.x ? #468

dartois-carlos opened this issue Jan 10, 2018 · 7 comments

Comments

@dartois-carlos
Copy link

Hi,

I'm having a problem using this bundle with Symfony 4.0.

  1. I install it via : composer require knplabs/knp-paginator-bundle
  2. I then add the basic configuration in my config file
  3. Finally I followed the example of the method "listAction ()"

When I execute my code, I find an error : "Service "knp_paginator" not found..."

I tried to directly inject the dependency into my method but without success.
Can you help me plz.

Thanks.
Carlos.

@nicolasmure
Copy link
Contributor

Hello,

Have you registered the bundle in your app kernel ?

Can you provide a snippet of your config file, and also the full error message that you have please ?

@dartois-carlos
Copy link
Author

Hi and thanks for your reply.

I'm using Symfony Flex so the bundle is automatically registered in the bundle.php file:
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],

Then I created a paginator.yaml config file in the config/packages folder of SF4 with the following:

knp_paginator:
    page_range: 5                       # number of links showed in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
    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
        filter_field_name: filterField  # filter field query parameter name
        filter_value_name: filterValue  # filter value query paameter name
    template:                                        
        pagination: '@KnpPaginator/Pagination/sliding.html.twig'     # sliding pagination controls template                                    
        sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template                                
        filtration: '@KnpPaginator/Pagination/filtration.html.twig'  # filters template

I have no error with this configuration but when trying to fetch knp_paginator from the container I got:

Service "knp_paginator" not found: even though it exists in the app's container, the container inside "App\Controller\Videos\PlaylistsController" 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 "PlaylistsController::getSubscribedServices()".

And I don't know how to "inject" this service directly in my controller (like I inject Request or others services).

@nicolasmure
Copy link
Contributor

nicolasmure commented Jan 10, 2018

I'm not able to reproduce your issue :( I did the same as you (using flex to install symfony/website-skeleton) and I am able to retrieve the service.
Can you post your controller file please ?

And also, what's the output of $ bin/console debug:container | grep paginator ?

@dartois-carlos
Copy link
Author

I'm just using $this->get('knp_paginator') in my controller and it don't work as expected.
Can you show me your code, maybe it will help me to understand how to inject Knp...

This is the output of the command:
capture d ecran 2018-01-10 a 12 34 16

@nicolasmure
Copy link
Contributor

nicolasmure commented Jan 10, 2018

// src/Controller/DefaultController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    public function testPaginatorAction(Request $request)
    {
        return $this->render('default/test-paginator.html.twig', [
            'pagination' => $this->get('knp_paginator')->paginate(
                ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
                $request->query->getInt('page', 1), // page number
                2 // limit per page
            ),
        ]);
    }
}
<!-- templates/default/test-paginator.html.twig -->
<!DOCTYPE html>
<html>
    <head>
        <title>Paginator test</title>
    </head>
    <body>
        <table>
        {# table body #}
        {% for item in pagination %}
        <tr {% if loop.index is odd %}class="color"{% endif %}>
            <td>{{ item }}</td>
        </tr>
        {% endfor %}
        </table>
        {# display navigation #}
        <div class="navigation">
            {{ knp_pagination_render(pagination) }}
        </div>

    </body>
</html>

and the bundle is also in config/bundles.php :

return [
    // ...
    Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],
];
# config/packages/paginator.yaml
knp_paginator:
    page_range: 5                       # number of links showed in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
    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
        filter_field_name: filterField  # filter field query parameter name
        filter_value_name: filterValue  # filter value query paameter name
    template:
        pagination: '@KnpPaginator/Pagination/sliding.html.twig'     # sliding pagination controls template
        sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template
        filtration: '@KnpPaginator/Pagination/filtration.html.twig'  # filters template

@dartois-carlos
Copy link
Author

Oh !

I found my error.
My controller does not extend Controller.

class PlaylistsController extends KinomapController

I fix it directly in my KinomapController :
class KinomapController extends AbstractController
to
class KinomapController extends Controller

Thx a lot !
Carlos

@nicolasmure
Copy link
Contributor

Nice!

I'm closing the issue then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants