I needed to have the same code in three different controllers. I felt bad about duplicating the code. It also felt strange to create a plugin and jump between the controller and the plugin.
Shared controller
In the controllers where you have duplicated code, (for example /site/controllers/home.php) do this:
You could also use a function that gets the search results (but you would have to return the result from the controller to the template). But at least you wouldn’t have to repeat the complete logic. Not more code in the controller than including a shared controller.
I know it’s an old topic but I want to point out in case someone asking : It’s still the same trick to do for Kirby 3.
Work also to return a variable.