# 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
base name | path |
---|---|
{{ file.baseName }} | {{ file.path }} |