# Creating custom subscriber Let's say we want to paginate a directory content, which might be quite interesting. And when we have such a handy **Finder** component in symfony, it's easily achievable. ## Prepare environment I will assume we you just installed [Symfony demo](https://github.com/symfony/demo) and you install [KnpPaginatorBundle](https://github.com/knplabs/KnpPaginatorBundle). Follow the installation guide on these repositories, it's very easy to set up. ## Create subscriber Next, let's extend our subscriber. Create a file named **src/Subscriber/PaginateDirectorySubscriber.php** ``` php target) || !is_dir($event->target)) { return; } $finder = new Finder(); $finder ->files() ->depth('< 4') // 3 levels ->in($event->target) ; $iterator = $finder->getIterator(); $files = iterator_to_array($iterator); $event->count = count($files); $event->items = array_slice($files, $event->getOffset(), $event->getLimit()); $event->stopPropagation(); } public static function getSubscribedEvents(): array { return [ 'knp_pager.items' => ['items', 1/* increased priority to override any internal */] ]; } } ``` Class above is the simple event subscriber, which listens to **knp_pager.items** event. Creates a finder and looks in this directory for files. To be more specific it will look for the **files** in the directory being paginated, max in 3 level depth. ## Register subscriber as service Next we need to tell **knp_paginator** about our new fancy subscriber which we intend to use in pagination. It is also very simple, add few line to your service config file (usually **config/services.xml**) ``` xml ``` ## Controller action Finally, we are done with configuration, now let's create actual controller action. Modify controller: **src/Controller/DemoController.php** And add the following action, which paginates the previous directory ``` php paginate(__DIR__.'/../', $request->query->getInt('page', 1), 10); return $this->render('demo/test.html.twig', ['pagination' => $pagination]); } ``` ## Template And the last thing is the template, create: **templates/demo/test.html.twig** ``` html {% extends "layout.html.twig" %} {% block title "My demo" %} {% block content %}

Demo

{# sorting of properties based on query components #} {# table body #} {% for file in pagination %} {% endfor %}
base name path
{{ file.baseName }} {{ file.path }}
{# display navigation #} {% endblock %} ``` Do not forget to reload the cache: **php bin/console cache:clear** You should find some files paginated if you open the url: **http://baseurl/index.php/demo/test**