Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How can I improve this flow between the frontend and the backend

Hello,

I deleted my previous question and made a (hopefully) one that is more clear with actual example and how it works right now and whether it can be made to work in a more convenient way:

I'd like to get some tips on how to better set my current "flow" between the front and the backend.

This is the situation: Assuming the page is at www.example.com/the-page, the user can update and view data on that page. The data is then saved in a session key based on the pathname and the parent element id. So if the data is displayed in:

**The data is a temporary filtered data in the browser, not something to save in the backend, that's why I use sessions

<div id="some-id">
    // . . data  . .
</div>

Then I save the related data in the key the-page_some-id (And it's unique thanks to Laravel's built-in session driver). The session key is sent by AJAX using JavaScript from that page. So far I only needed to use that session on that page itself, www.example.com/the-page, so I just got the pathname from the request, and all I need to know is the id (some-id) to get the relevant data. But now I need to get that data from other routes, like www.example.com/the-page/export-data-to-pdf or www.example.com/the-page/send-data-by-email. Now in those routes I can't just get the pathname from the request of these routes because it's different, so I also need to send the pathname in the request as an input or query string. But is there a better way to handle that? So if I initialize the JS script on www.example.com/the-page, I won't need to worry about knowing pathnames/ids to get the right session keys, but instead get the data of the relevant session more conveniently?

Example "flow":

In www.example.com/the-page:
<div id="some-id">
    // . . data  . .
</div>

<script>
$.ajax({
  method: "POST",
  url: "save-session",
  data: {
        id: "some-id",
        pathname: window.location.pathname.split('?')[0],
        info: // getting necessary info 
    }
})
  .done(function() {
    console.log("success")
  });
</script>    

Then in save-session:

public function saveSession(Request $request)
{
    $id = $request->input('id');
    $pathname = $request->input('pathname');
    $session_key = $pathname . "_" . $id;
    $info = $request->input('info');
    $data =   // updating $data using $info
    session([ $session_key  => $data]); // updating the session data
}

Then, whenever www.example.com/the-page loads, I want to get the relevant data to this page:

public function thePage(Request $request)
{
    $pathname = $request->path(); // the pathname in the request
    $id1 = 'some-id'; // hardcoded because I need to get the data relevant for that element
    $id2 = 'some-id-2'; // another id related to another element in that page
    
    $session_key1 = $pathname . "_" . $id1;    
    $session_key2 = $pathname . "_" . $id2;    

    $data1 = session()->get($session_key1);
    $data1 = session()->get($session_key2);
} 

But now I also need it in other routes like the export/email routes:

public function thePageExport(Request $request)
{
    $id = $request->input('id') 
    $request->input('pathname'); // need to manually pass the right URL
    $session_key = $pathname . "_" . $id;    
    $data = session()->get($session_key);
}

Is there a way to improve this flow so that I'd need to pass less variables for the relevant routes?

0 likes
1 reply
Snapey's avatar

why does the session key not reflect the type of data rather than where it came from?

Please or to participate in this conversation.