Redirect to different pages based on previous page in Laravel

Imagine that you have a method in the controller, for example, to store a new company. Also imagine that we can create a new company from different pages of our site. For example, I can create a company with 2-3 pages.

Depending on where I created the company, I need to have different redirects. Sometimes I need to redirect back, and sometimes to other routes.

Sort of:

if ($previousRoute === 'companies.index') { return redirect()->back(); } else { return redirect()->route('someroute'); } 

I think I cannot get the name of the route from which the user came. If I check the referrer URL, then if the route URL changes, everything will be broken, so I want to use the route names instead. Also, a solution that has a lot of if-s or switch looks weird, it will pollute the code.

Wildcard support is also required. Some sort of redirected route map or something like that.

Any tips on how to implement this?

+6
source share
3 answers

Well, you can compare the previous url with the URL of any route. This is not the best solution, and I do not know what to do with the route parameters, but it can work.

 if (URL::previous() === URL::route('companies.index')) { return redirect()->back(); } else { return redirect()->route('someroute'); } 
0
source

In some cases, you will need a set of if (or switch ) switch , since you have to make a decision.

I would not rely on the referrer URL, as it will break if you ever change your routes. Like you, I prefer route names for everything, as my URLs can change.

How about a session key? Set some value, then extract it and select the redirection after creating the company. Something like that:

In the controller method for creating company page # 1

 Session::put('create-company-method', 1); 

Then, after creating the company

 $flag = Session::get('create-company-method'); if ($flag===1) { return redirect()->route('go-here-next'); } else if ($flag===2) { ... } 

It is ugly, but it will be a trick. Plus it has a big advantage: you can make the session values ​​the way you want, so if you need to add additional information to make a decision, go ahead. Do you need wildcards? Set the session key, however you want to pass this information.

0
source

I could not leave if you had some kind of "logic" after routing after saving Company . In one of my applications, I needed to edit the address and redirect either back to the previous URL or back to the index page.

If your application has a fairly simple and clean solution.

We listen to the Laravel redirect()->intented() function.

In your GET function (e.g. getCreate ), we set the intentional URL for the previous URL if it does not match the URL itself:

 ($request->fullUrl() != URL::previous()) ? session()->set('url.intended', URL::previous()) : null; // url.intended is the same key that is used be Laravels intented function 

Now in your POST method (e.g. postCreate ) you can simply return the redirect to the intended URL as follows:

 return redirect()->intended(' AddressController@getIndex '); // The string within intended() defines where you want to be redirected if there is no session key is found 
0
source

All Articles