Hello I am referring to an older topic of mine:
AH just saw your answer… Perfect. Thank you
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?
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
Yes, can be achieved with $page->render()
.
1 Like
oh nice! I will give it a try. Thanks for this!
DennisA
December 10, 2019, 11:10am
8
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);
}
]
]
];
texnixe
January 23, 2020, 8:47am
10
I think there are several issues in your code.
The else statement in your first route does not return anything.
Is that a multi-language site or why are you using site()->visit()
?
The second route doesn’t check if the child exists before calling the url()
method.
DennisA
January 23, 2020, 8:59am
11
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);
}
]
]
texnixe
January 23, 2020, 9:06am
12
Now there’s a new error in this line:
$data = [
modal' => $page->url();
];
=> no semi-colon! Don’t you have debugging turned on?
texnixe
January 23, 2020, 9:10am
13
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
DennisA
January 23, 2020, 1:11pm
14
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>
texnixe
January 23, 2020, 2:02pm
15
How do you fetch the content?
DennisA
January 23, 2020, 2:12pm
16
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'));
});
texnixe
January 23, 2020, 5:21pm
17
Are you defining your routes in the config? Could you post your complete config, please?
DennisA
January 23, 2020, 7:43pm
18
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');
}
}
]
]
];
texnixe
January 23, 2020, 7:45pm
19
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.)
DennisA
January 23, 2020, 7:57pm
20
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!