Anonymous functions in controller returns to where?

I have no problems with using the controllers but I don’t fully understand why they work.

return function($site, $pages, $page) {
  return array(
    'nav' => 'Something',
  );
};

That is a working controller.

  1. It return an array with values to the function.
  2. Then it return the function, but to where? There are no variable to catch it.

The controller below also work:

$test = function($site, $pages, $page) {
  return array(
    'nav' => 'Something',
  );
};
return $test;

For me it’s a little more logical because now I’ve catched the output into $test.

Return to where?

Then there is a return $test and what I don’t understand is how it can return it to something. It’s not inside a function, it’s just inside a controller file.

That’s PHP magic. You/Kirby can catch it using this:

$return = require(CONTROLLER);

BTW: It’s actually the other way around: You first return the function, which will then get called and then return the array.

1 Like

Thanks! :smiley:

Now I’ve built controllers myself, for a plugin. Nice?