Controller for whole site

Hi,

I wonder if i can have (something like) a controller for my whole site?

Use case: I got some “main” pages (template = main) and some other pages with different templates and the navigation is split between these pages so it would be nice to have this controller loaded on any page to use it for my menu snippet:

<?php

return function($site, $pages, $page) {
    
    // find main pages
    $mainPages = $pages->filterBy('template', 'main');
    
    // find other pages
    $auxPages = $pages->filterBy('template', '!=', 'main');
    
    return compact('mainPages','auxPages');
    
};

I could add this to the snippet but at some pages there’s more like one navigation and I’d like not to repeat myself …

http://forum.getkirby.com/t/shared-controllers/1112

I asken a similar question. Maybe it might give some answers?

Thanks … I didn’t had the right keywords for my search and missed your thread …

I thought on plugins and tried to simply move the above code to the pluginsfolder but I can’t access $mainPages and $auxPages in my templates … using only the two lines defining variables with out the function wrapper doesn’t work either.

Is because the $page object is not yet loaded?

http://forum.getkirby.com/t/functions-unable-to-load-the-page-object/1081

In this case I still don’t have a good solution for it.

No even

<?php

$testVar = 'xx';

as plugins/siteController.php don’t work (undefined variable $textVar). Adding compact('testVar'); don’t help. I guess it has something to do with variable scopes, but I don’t know PHP and Kirby’s internals well enough …

Maybe this could be a solution to this issue?

http://forum.getkirby.com/t/shared-controller/1839