Routing Array Error

Hello I am referring to an older topic of mine:

I am currently updating my website from Kirby 2 to Kirby 3. I tried to change my routing with the new syntax. But I only get errors.

Here is what I want to achieve:

  • I am using ajax to load a page content into an overlay
  • I also want to get to this state with a direct URL access

This is how my routing looks like:

<?php

return [
	'routes' => [
	    [
	      'pattern' => 'projects/(:any)',
		  'action' => function($modal) {
				$kirby = 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'
	    ],
	    [
		    '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);
				}
			}
		]
	]
];

When I enter the url I only get a blank page with the following line:
["home",{"modal":"http:\/\/localhost:8888\/studio_oui_r\/projects\/test"}]

Yep, an array is now returned as a JSON array, ran into that recently as well, but can’t find anything in the docs or my brain how to solve this to return additional data to the page.

Thanks for your answer. Oh no. So there is currently no solution/workaround for this? :frowning:

I’m not saying that, I just don’t know.

Hi @texnixe. Just wanted to ask if you could find a solution for this? … I didn’t get any further on this one. Maybe there is a solution now :slight_smile:

Yes, can be achieved with $page->render().

1 Like

oh nice! I will give it a try. Thanks for this!

I finally had some time time to try this, but I can’t get my head around it. I keep getting a blank page.

my routing looks like this now:

<?php

return [
	'routes' => [
	    [
	      'pattern' => 'projects/(:any)',
		  'action' => function($modal) {
				$kirby = 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'
	    ],
	    [
		    'pattern' => 'projects/(:any)',
			'action' => function($modal) {
				$page = page('projects')->find($modal);

				$data = [('modal' => $page->url())];
				return page('home')->render($data);

			}
		]
	]
];

no one? :disappointed_relieved: :pray:

I think there are several issues in your code.

  1. The else statement in your first route does not return anything.
  2. Is that a multi-language site or why are you using site()->visit()?
  3. The second route doesn’t check if the child exists before calling the url() method.

Thank you for your fast answer. It’s not a multi-language site, it somehow worked for Kirby 2 (It took me super long to get this working) but I have no idea why its not calling in version 3. Here just to compare the two:

For Kirby Version 2:

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

And here for Version 3 :

'routes' => [
	    [
	    	'pattern' => 'projects/(:any)',
			'action' => function($modal) {
				$kirby = 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'
	    ],
	    [
		    'pattern' => 'projects/(:any)',
			'action' => function($modal) {
				$page = page('projects')->find($modal);
				if($page){
				$data =[
					'modal' => $page->url();
					];
				} else {
					$page = site()->errorPage();
					site()->visit($page);
				}
				
				return page('home')->render($data);

			}
		]
	]

Now there’s a new error in this line:

$data = [
  modal' => $page->url();	
];

=> no semi-colon! Don’t you have debugging turned on?

The second route should look like this:

  [
            'pattern' => 'projects/(:any)',
            'action' => function ($modal) {
                $page = page('projects')->find($modal);
                if ($page) {
                    $data = [
                        'modal' => $page->url()
                    ];
                    return page('home')->render($data);
                } else {
                    return go('error');
                }
            }
    ]

And the first:

    [
            'pattern' => 'projects/(:any)',
            'method' => 'POST',
            'action' => function ($modal) {
                $kirby = kirby();
                $page = page('projects')->find($modal);

                if ($page) {
                    $html = $page->render();
                    return new Response($html, 'text/html');
                } else {
                    return false;
                }
            }
            
        ],
1 Like

Thank you very much… my debugging was on but I didn’t get any error.

I tried the version you sent but it still directs me on the homepage instead of opening the project in my modal. Maybe something else here which changed with the new Kirby version?

The modal which the project is opening:

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

How do you fetch the content?

I fetch it via AJAX:

var startModal = $('#modal-container').data('modal');

if (startModal) {
	openUrlInModal(startModal, $('#modal-container'));
}


function openUrlInModal(url, target){
	$.ajax({
		url: url,
		type: 'POST',
		success: function(data) {
			$(target).append(url).addClass('projectoverlay');
			$('#modal-container').html(data);

		}
	});
}

$("body").on("click touch", ".load", function (event) {
	event.preventDefault();

	fetchedProjectUrl = $(this).data('url');
	
	openUrlInModal(fetchedProjectUrl, $('#modal-container'));
});

Are you defining your routes in the config? Could you post your complete config, please?

Yes sure. Here is my config:

<?php

return [
	'debug' => true,
	
	'routes' => [
	    [
            'pattern' => 'projects/(:any)',
            'method' => 'POST',
            'action' => function ($modal) {
                $kirby = kirby();
                $page = page('projects')->find($modal);

                if ($page) {
                    $html = $page->render();
                    return new Response($html, 'text/html');
                } else {
                    return false;
                }
            }
            
        ],
		[
            'pattern' => 'projects/(:any)',
            'action' => function ($modal) {
                $page = page('projects')->find($modal);
                if ($page) {
                    $data = [
                        'modal' => $page->url()
                    ];
                    return page('home')->render($data);
                } else {
                    return go('error');
                }
            }
		]
	]
];

Ok, thanks. Do you get any errors now or what exactly happens? PHP errors? Errors in your console?

And what do you get if you sent a post request to that route (using a browser add in or app like Rested, Postman etc.)

I do not get any errors. When I enter a projects url it is redirecting me to the homepage instead of opening it in the modal, but the projects url is still in the browser bar…

never had experience with rested or postman, but I will give it a try!