How to pass data between controllers

Is there a way to pass data between controllers?

I’m building a little webshop and am storing all the data between product pages in a session, which then I call up in a controller.

But I am also using kirby uniform to collect buyer’s informations before the order is being placed. And I am doing this in a different page (checkout page) than where the form is (cart page).

I guess I can store also this informations in a session, or doing something else?

The only other option that comes to mind is to save the form in a page.txt and then loop over those values again to save them in another page.txt (for the final order page, with buyer’s infos and order infos — quantity, price, etc).

I’d like to keep both the order infos and buyer’s info in one page and create a new array by merging both dataset and then create a new page/file with that.

I would think storing it on the file system, even temporarily, is a security & data protection risk. The file contains a certain amount of personal information. If you must store it, do it in the session.

I guess you could use local storage but would have to use Javascript to get / set the values because its client side.

Alternatively, why not make the checkout page and the cart page the same page and make the page a wizard of some kind? Something like these.

Hey, thanks for replying as usual — took a break of sort from client projects the past two weeks.

Yes, I combined both cart and registration form in the same template. How do I go about binding Uniform’s send button w/ a hook that redirects the cart page to the checkout page right after you clicked on it?

I’m looking up the redirect::to() command atm.

I take it you want the checkout page to handle the form submission, right? Then all you need to do is set the form’s action attribute to the checkout page.

Right! That worked.

Thanks.


OK so, I moved the Uniform code in the cart template, but I still want to merge the form data with the checkout data (which handles the order’s informations).

I realise only now why two weeks ago I was asking this…

I see Uniform offers the SessionStore Action, and I did as they suggest, but the whenever I call the session, it says it’s null.

In the controller where Uniform is (cart.php):

if (r::is('POST')) {
      $form->sessionStoreAction(['name' => 'user-form']);

    /* email action {...} */
}

In the other controller (checkout.php):

$form = s::get('reg-form');

/* then for eg */
$ff = a::merge($form->data(), $new_order);

What am I missing?

I already have a session going on in /plugins/cart/cart.php which I then called in /controllers/cart.php. Is this interfering?

how about using sessions the regular way first? i found uniform hard to transision to when u are already used to the basic stuff…

s::set('name', get('name'));

s::get('name');
1 Like

Yes, will give it a try!

Are these two different forms you are storing in the session or why do you store user-form in the session and then try to get reg-form from the session?

That was a type, user-form and reg-form are one session (reg-form). I’m back to work on this today, tried again but not working.

I also tried to use the basic kirby form function.

In the controller that handles the form, I put

return function($site, $pages, $page) {
  s::set('form', get('form'));

  (...)

in the controller where I want to merge the two sessions, I’m doing this

$form = s::get('form');
$ff = a::merge($form, $new_order);
a::show($ff);

I admit that doing s::set('form', get('form')) does not look right to me… and it’s probably not correct?

Do you ever start the session? Does dumping any of these s::get()s return anything?

It does not return anything. Should not the Uniform function start the session automatically, or do I have to explicitly set s::start(); somewhere?

Dumping s::get() does not output anything.

s::start();

$array1 = [1,2,3];
s::set('test', $array1);



$array2 = [5,6,7];
s::set('new', $array2);

$merge = array_merge(s::get('test'), s::get('new'));
dump($merge);
?>
1 Like

this is working! looking up how to convert Uniform’s form object into an array now…

Today I tried again to use Uniform’s session method, and it worked

(controller-a.php)
if (r::is('POST') && get('ck') === 'Submit') {
  $form->sessionStoreAction(['name' => 'user-form']);
}

then

(controller-b.php)

$userform = s::get('user-form');
a::show($userform->data('email'));

Today I had to accidentally rewrote the Uniform code, not sure if something was missing the other day, or if I was still hungover from the weekend :stuck_out_tongue:

yay!