# Creating custom subscriber Lets say we want to paginate a directory content, might be quite interesting. And when we have such a handy **Finder** component in symfony, its easily achievable. ## Prepare environment I will assume we you just installed [symfony-standard](https://github.com/symfony/symfony-standard) edition and you install [KnpPaginatorBundle](https://github.com/knplabs/KnpPaginatorBundle). Follow the installation guide on these repositories, its very easy to setup. ## Create subscriber Next, lets extend our **AcmeDemoBundle** which comes together with **symfony-standard** edition. Create file **../symfony-standard/src/Acme/DemoBundle/Subscriber/PaginateDirectorySubscriber.php** ``` php target) && is_dir($event->target)) { $finder = new Finder; $finder ->files() ->depth('< 4') // 3 levels ->in($event->target) ; $iter = $finder->getIterator(); $files = iterator_to_array($iter); $event->count = count($files); $event->items = array_slice( $files, $event->getOffset(), $event->getLimit() ); $event->stopPropagation(); } } public static function getSubscribedEvents() { return array( 'knp_pager.items' => array('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, create additional service config file: **../symfony-standard/src/Acme/DemoBundle/Resources/config/paginate.xml** ``` html ``` ## Load configuration into container Now to finish this configuration we need to load it from our dependency injection extension. Modify file: **../symfony-standard/src/Acme/DemoBundle/DependencyInjection/config/AcmeDemoExtension.php** ``` php load('services.xml'); // loading our pagination services $loader->load('paginate.xml'); } ``` ## Controller action Finally, we are done with configuration, now lets create actual controller action. Modify controller: **../symfony-standard/src/Acme/DemoBundle/Controller/DemoController.php** And add the following action, which paginates the previous directory ``` php get('knp_paginator'); $files = $paginator->paginate( __DIR__.'/../', $this->get('request')->query->getInt('page', 1), 10 ); return compact('files'); } ``` ## Template And the last thing is the template, create: **../symfony-standard/src/Acme/DemoBundle/Resources/views/Demo/test.html.twig** ``` html {% extends "AcmeDemoBundle::layout.html.twig" %} {% block title "Symfony - Demos" %} {% block content_header '' %} {% block content %}

Available demos

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