Accessing collection defined in controller from snippet returned in a route

I do ajax-style loading of snippets using a route, as such:

	'routes' => [
		[
			'pattern' => '(:all)echo',
			'method' => 'post',
			'action'  => function () {
				$snippet = get('snippet');
				return json_encode(snippet($snippet, [], true));
			}
		],

Then I fetch that and use it to replace innerHTML of a certain div on the page, with something like:

		fetch(url, {
			method: 'post',
			body: JSON.stringify({snippet: snippet}),
			headers: {'Content-type': 'application/json '}
		}).then(function (response) {
			return response.json();
		}).then(function (data) {
			document.querySelector(container).innerHTML = data;

Which works well, but some returned snippets use collections that are defined in site.php controller, such as these collections:

	$allExhibitions = page('exhibitions')->children()->sortBy('openingdate', 'desc');

	$upcomingExhibitions = $allExhibitions->filterBy('openingdate', 'date >', 'today');
	$currentExhibitions = $allExhibitions->filterBy('openingdate', 'date <=', 'today')->filterBy('closingdate', 'date >=', 'today');
	$pastExhibitions = $allExhibitions->filterBy('closingdate', 'date <', 'today');

What would be a good way to pass these collections to the snippet when returning it from config, or for the snippet to access these collections.

In the past I reassigned needed collections directly on the config page, but in this case that is not necessary, and I would be assigning them twice, on controller and on config.

Thank you

Why don’t you define them as collections?

Thank you @pixelijn,

Initially because AFAIK I’d need a file per collection instead of defining all collections in a single file, and I’d then have to conditionally pass the required collections according to the fetched snippet, which complicated the code.
-> It does seem that one collection -> one file, but I the snippets retourned by a route already have access to the collections, I don’t need to pass the collections to them as array arguments as I wrongly thought.

Additionally, there are quite a few collections there that derive from an initial collection, so I was unsure about order, in the sense that I did not know if $collectionA would be available inside $collectionB file, or the other way around.
->From my tests I’d say the order in which collections are defined (possibly alphabetic order) is irrelevant

Finally because I am unsure on how to access a collection in a route, I’ve tried returning collection(‘test’), $kirby->collection(‘test’) and kirby()->collection(‘test’) and I get whoopses.
-> Solved with kirby()->collection(‘test’)

Do you know the answers to these doubts?

Thank you again