Get param on controller when returning snippet from route

This is Kirby 2, and I need to use params() on a site controller that passes a variable to a snippet that is loaded via ajax.

I explain:

I do a jquery $.post call agains a router in config. The router finds the right snippet and returns it encoded, then I add it to the page via jquery .html()

JS:

$.post(
	baseurl + "/eco",
	{
	    snippet: snippet,
	    page: page
	}, function(data){
        $(container).html(data);
	}, "json");

Route:

c::set('routes', array(
	array(
		'pattern' => 'eco',
		'method' => 'POST',
		'action'  => function() {
			$dasSnippet = get('snippet');
			$dasPage = get('page');
			try {
				return json_encode(snippet($dasSnippet, array('p' => $dasPage), true));
			} catch (Exception $ex) {
				return $ex->getMessage();
			}
		}
	)
));

There is a site controller that defines and returns variables that are used in the loaded snippets. And this site controller uses param() to conditionally define certain variables. When the snippet is loaded via ajax, from my tests it seems all variables coming from the controller are available to the ajax loaded snippet, except the one that depends on param().

In other words, param() does not seem to work when ajax loading the snippet in this manner.

This is the site controller:

<?php
return function($site, $pages, $page) {

	$latestEdition = page('editions')->children()->visible()->first();

	/* If there is an archive parameter in the URL use it to set currentEdition, otherwise current is latest*/
	if (param('archive')) {
		$currentEdition = page('editions')->children()->find(param('archive'));
		$archive = true;
	} else {
		$currentEdition = $latestEdition;
	}
	$currentThemeText = $currentEdition->find('theme-text');
	$currentExhibitions = $currentEdition->find('exhibitions');
	$currentEvents = $currentEdition->find('events');
	$currentAssProgram = $currentEdition->find('associates-program');
	$currentWriter = $currentEdition->find('writer-in-residency');

	// pass variables to the template
	return compact(
		'currentEdition',
		'currentThemeText',
		'latestEdition',
		'currentExhibitions',
		'currentEvents',
		'currentAssProgram',
		'currentWriter',
		'archive'
	);

};

Are my expectations here correct ? am I doing something wrong?

I will add here how am I testing this.

In the controller I define variables inside and outside of the param() function. This variables have a time stamp attached to them so I can be sure of when are they defined:

<?php

return function($site, $pages, $page) {

	$inParamTest = 'false';
	$outParamTest = 'true' . time();
	if (param('archive')) {
		$inParamTest = 'true' . time();
	}

	// pass variables to the template
	return compact(
		'inParamTest',
		'outParamTest',
	);

};

Then in my snippet I dump both variables:

<?php dump('inParamTest: ' . $inParamTest) ?>
<?php dump('outParamTest: ' . $outParamTest) ?>

If I load an URL with an archive param via HTTP I get:

inParamTest: true1609353772
outParamTest: true1609353772

If I load using $.post and the route I get:

inParamTest: false
outParamTest: true1609354110

If I am understanding this correctly outParamTest is defined in site controller and available to the snippet in both http load AND $.post load. While inParamTest is only defined OR available to the snippet in http load.

Which would mean that I can use the controller to load variables in route-returned ajax-loaded snippets, but somehow param() does not work. Perhaps it does not have access to the URL.

A possible workaround would be to pass the parameter via $.post call to the route, and from the route into the controller, where it is used if param() can’t do its job, something like:

if (param('archive')) {
    // use param('archive')
} elseif (isset($postedParam)) {
	// use $postedParam
} else {
	// none available
}

…but I’d like to understand what’s going.

May it be that the URL is simply not available to site controller and into param() because the url is not re-loaded on ajax call ?

My code is wrong. The router , as it is coded, does not work. My post call is being received somwhere else. I am going to open a different issue to solve firs the router code, then come back if necessary.

In the end I get the param in JS and include it in the $.post call, which is done against a template instead of a route (as I don’t even know if that is possible in k2):

JS:

		var ajaxArchive = '';
		var urlLastBit = window.location.href.split("/").pop();
		if (urlLastBit.startsWith('archive:')) {
			ajaxArchive = urlLastBit.split(':').pop();
		};
		$.post(
			// 
			baseurl + "/api",
				{
				snippet: snippet,
				page: page,
				ajaxArchive: ajaxArchive
			}, function(data){
				$(container).html(data);
			}, "json");

templates/api.php

<?php
if(kirby()->request()->ajax()) {
	$data[] = snippet($_POST["snippet"], array('p' => $_POST["page"], 'ajaxArchive' => $_POST["ajaxArchive"] ), true);	
	echo json_encode($data);
}

The snippet receives the $ajaxArchive variable and uses it to define $currentEdition

<?php 
if(isset($ajaxArchive) && strlen($ajaxArchive) > 0) {
	$currentEdition = page('editions')->children()->find($ajaxArchive);
} 
?>

Hopefully back to k3 , never again to look back.

cheers