How to Return Back Twice in Laravel

How to return back twice in Laravel?

No, but you could use session system to save URLs of 2-3-4 pages back. Use Session:: facade or session() helper for shorter syntax:

$links = session()->has('links') ? session('links') : [];
$currentLink = request()->path(); // Getting current URI like 'category/books/'
array_unshift($links, $currentLink); // Putting it in the beginning of links array
session(['links' => $links]); // Saving links array to the session

And to use it:

return redirect(session('links')[2]); // Will redirect 2 links back

How to redirect to two step backwards in Laravel?

You can use the Session system to save the URL all pages back. Check below steps to redirect 2 or 3 backward URL redirect.

1) First, you can get the all URLs from session system variable.

$urls = array();
if(Session::has('links')){
$urls[] = Session::get('links')
}

2) Then get the current page url.

  $currentUrl = $_SERVER['REQUEST_URI'];

3) Mapping with current url to other all url.

array_unshift($urls, $currentUrl);
Session::flash('urls', $urls);

4) Get all Links fetch from session system like below

  $links = Session::get('urls'); 

5) Then You can redirect to a particular page.

   return redirect($links[2]); 

Redirect two pages back with url()- previous() in laravel

The previous() method returns the user's last location within the app, not the last URL the browser accessed.

To do that, in Laravel, you can try Request::server('HTTP_REFERER'), or in plain PHP, $_SERVER['HTTP_REFERER'].

That said, be aware that HTTP_REFERER is not exactly the most reliable value. It can be spoofed (faked) easily or not provided at all, so test accordingly.

If possible, the best option would be to add a GET parameter to the link present in the remote site, so that when you receive the request in your Controller, you have the means to identify where the user is coming from.

Laravel 5.4: controller method is called twice on a redirect to it

Have you tried passing values in parameters? Try the below code.

public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()->action(
'SampleController@confirmUser', ['cvId' => $cvId, 'userId'=>$user->id]
);
}

public function confirmUser(Request $request) {
$cvId = $request->cvId;
$userId = $request->userId;

if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}

// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}


Related Topics



Leave a reply



Submit