Ok so, I have a little special Kirby setup where I store products in a database and use custom routes to build up the urls for the product pages. I then have a category tree menu build by pages and subpages that show a collection of products based on some tags stored along the products in the db. The category tree menu is “closed” from the beginning and sub categories is shown when the user clicks on a category and go to that page.
When a user then decides to click on one of the products they go to the product page thanks to the custom router that pull the right product data from the db. But on this page the category tree menu is back to its closed state because kirby does not know what category the user clicked on before choosing a product. How do I best solve this?
I prefer not to use url params for this due to seo and keeping the product url clean. So I thought sessions would be a good ide to store the uri of the last category that was open before going to the product page. So at the top of my category template I put
$kirby->session()->set('priskriget.categoryHistory', $page->uri());
$categoryHistory = $kirby->session()->get('priskriget.categoryHistory', false);
echo $categoryHistory; // just to see what got stored
The first time I go to a category page I can see that that uri is stored in the session, perfect. Then when I click on a product and go to the product page that same uri is still in the session. Nice! But then if i reload the product page the uri from the session get changes to another category uri. Why? Is the session code from my category template running on the product page? I probably just don’t understand how sessions works but a little help on this would be much appreciated.
Hm, according to the documentation, pull() fetches the current value and then removes it. While that doesn’t explain why you still get a value, you should probably use get() if you want the value to still be there after page reload.
Do you have any steps to reproduce this in a Starterkit? If I set a session value in one template and retrieve it in another template, then I can reload the page as many times as I want without the value disappearing. So I’d assume you have some code somewhere that sets a different value. But hard to tell without knowing what’s happening in your code.
You are right. In the startkit it works as you expect. I can’t under stand what is happening, it’s super weird. If I in the product template change to another session name
and now reload the product page twice (first the $categoryHistory is empty) the uri “home” is displayed. So it’s like the code from the home template is running even on the product page. Don’t even know where to start to find this bug.
Looks like it might be something with my local server setup. I use MAMP Pro and if I use the “localhost/sitefolder” url it all works, but if I set up a custom local domain (host) in MAMP and point that to the sitefolder and use that local domain in the browser the weird reload bug is back.
I find the bug! I forgot that I use a little js (https://getquick.link/) to preload content from links based on what is in the user’s viewport. So thats what made the session update to random uri