欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Redirect to the second last page How to return back twice in Laravel? 两次返回 像 js 里面的 history.go(-2) 有大用

In Laravel, there is a function return back();, which returns the user to the previous page. Is it possible to return back(); more than once within one function to return the user back twice or several times? I tried

public function ....()
{
  return back();
  return back();
}

but it doesn't seem to work.

    1 Answer 正确答案

    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

    来自  https://stackoverflow.com/questions/36098589/how-to-return-back-twice-in-laravel

    Redirect to the second last page

    Hi,

    i'm not sure how to solve this:

    1. I list all customers (with sorting options via URL query ?sort=...&city...)

    2. I click on "Create new customer"

    3. I save the new customer

    4. I want to redirect to step 1 with a) the same sorting settings AND with the new user (of course only displayed if he fits into the search request).

    Inside MyController():

    public function saveCustomer()
    {
    
    // save data into db
    
    return Redirect::action('MyController@listCustomers');
    
    }
    

    Okay this just redirects to "List all customers" without any sorting :(

    Instead of this i tried

    return Redirect::back();
    

    but this redirects only to the last page (the new-customer-form).

    So i need something like javascript history go back(-2) but with a refresh of that url.

    Maybe put the referer into a hidden field inside the form and send it to the saveCustomers controller?

    return Redirect::to(Input::get('referer'));
    

    Any other ideas?

    Thanks, Sharping

    sayasuhendra replied 3 years ago

    just type this

    echo '<script type="text/javascript">'
    			   , 'history.go(-2);'
    			   , '</script>';
    
    Alkimisti replied 3 years ago

    I would not rely on JavaScript code as JS may be disabled in a browser. Instead, at step 2 I would put:

    Session::flash('url',Request::server('HTTP_REFERER'));  
    

    and then at step 4 I would redirect to the URL which is saved in the session variable:

    return Redirect::to(Session::get('url'));  
    
    tuyenlaptrinh replied 3 years ago

    sayasuhendra said:

    just type this

    echo '<script type="text/javascript">'
      		   , 'history.go(-2);'
      		   , '</script>';
    

    Your solution will redirect to second last page but can not load new customer.

    Alkimisti said:

    I would not rely on JavaScript code as JS may be disabled in a browser. Instead, at step 2 I would put:

    Session::flash('url',Request::server('HTTP_REFERER'));  
    

    and then at step 4 I would redirect to the URL which is saved in the session variable:

    return Redirect::to(Session::get('url'));  
    

    This solution is mean When you in step one, you flash session url now and you can get session url after submit new customer

    bikithalee replied 3 years ago
    appkr replied 3 years ago

    Flash url in YourController@index

    Session::flash('backUrl', Request::fullUrl());
    

    Keep the sub session key in YourController@create and YourController@store

    if (Session::has('backUrl')) {
        Session::keep('backUrl');
    }
    

    Consume the session value in YourController@store or any subsequent views

    // YourController@store
    return ($url = Session::get('backUrl')) 
        ? Redirect::to($url) 
        : Redirect::route('any.named.route');
    
    // or any views
    @if ($url = Session::get('backUrl'))
        <a href="$url">Back to List</a>
    @endif
    
    joviermark replied 2 years ago
    jeremybalog replied 2 years ago

    Step 1 uses get tokens. Simply save the full URL in a session variable and use it to redirect in step 4. No need to over think it.

    divostar replied 2 years ago

    appkr said:

    Flash url in YourController@index

    Session::flash('backUrl', Request::fullUrl());
    

    Keep the sub session key in YourController@create and YourController@store

    if (Session::has('backUrl')) {
       Session::keep('backUrl');
    }
    

    Consume the session value in YourController@store or any subsequent views

    // YourController@store
    return ($url = Session::get('backUrl')) 
       ? Redirect::to($url) 
       : Redirect::route('any.named.route');
    
    // or any views
    @if ($url = Session::get('backUrl'))
       <a href="$url">Back to List</a>
    @endif
    

    Your answer solve my problem. Thanks a bunch

    来自  https://laravel.io/forum/08-03-2014-redirect-to-the-second-last-page

    普通分类: