vincent15000's avatar

Looking for a clean way to add a referer link

Hello,

I have this situation in a full livewire application.

I can call a create/edit form from 3 different pages.

If I click on the submit button to edit an existing model, I want to redirect to the referer page.

I have checked in the request, there is the referer link.

But => if the form has errors, the referer becomes the form.

What is the best way to add the referer ?

Adding it in the headers ? Adding a variable in the session ? Another way ?

Thanks for your help.

V

0 likes
9 replies
vincent15000's avatar

I think that the easiest way is to use the session.

Snapey's avatar

beware, session can be shared by two tabs in the browser. If your user tries to submit two forms at once it could get confusing

1 like
vincent15000's avatar

@Snapey I don't see why a user would want to open 2 forms at once from 2 different places and fill both forms at once. But effectively it's a good observation.

What other solution are you using ?

vincent15000's avatar

@Snapey @jlrdw I also thought about a simple variable in my livewire component. I open the form and it will store the referer in a variable while mounting the component.

Snapey's avatar
Snapey
Best Answer
Level 122

@vincent15000 I don't understand the problem, but passing it directly is ideal.

1 like
vincent15000's avatar

@Snapey

If I am on the show page and I click on the edit button, the referer URL is the one of the show page.

I fill the form and I click on the save button, but there are validation errors, the edit page is reloaded and the referer URL is now the edit page.

How can I return to the show page ?

I can't hard write the show page route for the redirection, because I can access the edit page also from the index page and not only from the show page.

I have found a solution : I can add the referer as a query parameter to the URL. So there isn't any problem with a session shared between 2 tabs.

martinbean's avatar

I fill the form and I click on the save button, but there are validation errors, the edit page is reloaded and the referer URL is now the edit page.

How can I return to the show page ?

@vincent15000 If you’re editing a record, then surely you know where to redirect to?

Why are you relying on the referrer? Just redirect to the associated record’s show page:

return redirect()->route('record.show', $record);
1 like
vincent15000's avatar

@martinbean

I can edit the same model from two different pages, so no I don't know where to redirect.

I explain.

It's a personal project that helps me follow students in training.

I can display the student page (with all training sessions of this student) and I have another page with all training sessions in a month for all students.

I can edit a session from the student page or from the sessions page.

  • if I edit a session from the student page, I want to redirect back to the student page

  • if I edit a session from the sessions page, I want to redirect back to the sessions page

Please or to participate in this conversation.