Routing Array Help

I use ajax to load page content into an overlay and the routing function to get to this state with a direct URL access. It works exactly the way I want but only for one page:

c::set('routes', array(
array(
	'pattern' => 'projects/(:any)',
	'action' => function($modal) {
		$kirby = new Kirby();
		$page = page('projects')->find($modal);
		
		if($page) {
			$html = $kirby->render($page);
			return new Response($html);
		} else {
			$page = site()->errorPage();
			site()->visit($page);
		}	
	},
	'method' => 'POST'
),
array(
	'pattern' => 'projects/(:any)',
	'action' => function($modal) {
		$page = page('projects')->find($modal);
		if($page) {
			$data = array('modal' => $page->url());
			return array('home', $data);
		} else {
			$page = site()->errorPage();
			return site()->visit($page);
		}
	}
),));

But as soon as I try to add this for another page like this:

c::set('routes', array(
array(
	'pattern' => 'projects/(:any)',
	'action' => function($modal) {
		$kirby = new Kirby();
		$page = page('projects')->find($modal);
		
		if($page) {
			$html = $kirby->render($page);
			return new Response($html);
		} else {
			$page = site()->errorPage();
			site()->visit($page);
		}	
	},
	'method' => 'POST'
),
array(
	'pattern' => 'projects/(:any)',
	'action' => function($modal) {
		$page = page('projects')->find($modal);
		if($page) {
			$data = array('modal' => $page->url());
			return array('home', $data);
		} else {
			$page = site()->errorPage();
			return site()->visit($page);
		}
	}
),
array(
	'pattern' => 'contact',
	'action' => function($modal) {
		$kirby = new Kirby();
		$page = page('contact')->find($modal);
		
		if($page) {
			$html = $kirby->render($page);
			return new Response($html);
		} else {
			$page = site()->errorPage();
			site()->visit($page);
		}	
	},
	'method' => 'POST'
),
array(
	'pattern' => 'contact',
	'action' => function($modal) {
		$page = page('contact')->find($modal);
		if($page) {
			$data = array('modal' => $page->url());
			return array('home', $data);
		} else {
			$page = site()->errorPage();
			return site()->visit($page);
		}
	}
),));

I get this error message “Missing argument 1 for Kirby::{closure}(), …”

This is how my modal div looks like:

<div id="modal-container" data-modal="<?php echo isset($modal) ? $modal : '' ?>" class="modal"></div>

Would be super nice if someone can tell me what I am doing wrong here.

Thanks in advance :slight_smile:

The $modal argument for the contact pattern doesn’t make sense, because it doesn’t refer to a placeholder. What is it supposed to be? If you want it to refer to the contact page, you would have to make your contact pattern a placeholder. But then page('contact')->find($modal)wouldn’t make sense anymore, because I don’t think your contact page has a subpage called contact? So what are you trying to fetch here?

OH okay. Thanks for your answer. I’ve read it like this: when the url matches “contact” than open the page into the modal overlay. I dont understand it :open_mouth:

Ok, then your routes should look like this:

array(
	'pattern' => 'contact',
	'action' => function() {
		$kirby = new Kirby();
		$page = page('contact');
		
		if($page) {
			$html = $kirby->render($page);
			return new Response($html);
		} else {
			$page = site()->errorPage();
			site()->visit($page);
		}	
	},
	'method' => 'POST'
),
array(
	'pattern' => 'contact',
	'action' => function() {
		$page = page('contact');
		if($page) {
			$data = array('modal' => $page->url());
			return array('home', $data);
		} else {
			$page = site()->errorPage();
			return site()->visit($page);
		}
	}
)

Nice! This works! Thanks a lot!

I think I also understand this now when I compare it with your answer before.
So ->find($modal) would only for subpages?

Exactly. If there are no subpages, there is nothing to find.

If you want to apply the same route for multiple pages, not only contact, you can do it like this:

array(
	'pattern' => '(contact | privacy | imprint)', // using placeholder syntax in the pattern
	'action' => function($uid) { // pass the placeholder as argument to the closure
		$kirby = new Kirby();
		$page = page($uid); // fetch the page using the variable
		
		if($page) {
			$html = $kirby->render($page);
			return new Response($html);
		} else {
			$page = site()->errorPage();
			site()->visit($page);
		}	
	},
	'method' => 'POST'
)

AH just saw your answer… Perfect. Thank you :slight_smile: