Check if controller exists

Hi there, I was wondering, if there is any way to check if a certain controller exits? No matter if that belongs to any plugin or just the site itself.

Going through the codebase, I saw that there is $kirby->controllerLookup() but unfortunately it’s not a public method…

Then there is $kirby->controller( $name ), but there seems no proper way to provide a fallback. (AFAIK site is the fallback, but in a Kirby sense site is not a page, and I use that already in another way.) It would be best if I could have something like default as fallback.

For this project I am going all headless and am not using templates, thus I cannot rely on $page->template()->name(), but still want to use controllers for some page types.

Thank you for any ideas.

There seems to be no method you can use in core code.

Maybe you can create your own helper method inspired by the controllerLookup() method :thinking:

Thank you for your reply!
True, I thought of the same, but then realized, that the function would then run at least twice (for checking and calling the controller) :wink: that felt weird so I wanted to ask first.

BTW: Why is controllerLookup() private anyway? Is it sensitive? Or could it maybe be a public method in the future?

I can’t see a reason for not being public. Pinging @lukasbestle to get ideas.

@moritzebeling Two questions so I can better understand your use-case:

  1. How do you plan to use the check if the controller exists? What kind of logic would differ between the case “controller exists” and “no controller exists”?
  2. How do you use the site controller at the moment if not as a default controller? Because that’s indeed what it is: We couldn’t name it default as that is the controller for the default template. So site was the only name that is guaranteed to be available for this purpose.

Thank you! Ah okay, I See :slight_smile:

I am using the controllers to process information I later want to deliver via an api endpoint.

The site controller returns basic information about the site, e.g. title, host, main navigation, languages, settings, metadata, … general things I need in the frontend application.

My idea was, to then optionally have pages controllers with the same name as the blueprint to process their dataset, but: if there is no controller for a specific name, take e.g. default. That’s what started my research leading to controllerLookup().

But maybe I’ll be better off, if I just use site as default page controller, and call the my current site controller website or siteinfo or something…

Yep, that’d be my recommendation as well.

The reason why the controllerLookup() method is protected is that it’s an internal method. The more methods we make public, the more we need to be careful about breaking-changes.