back()" to redirect back to the previous page; 3. Redirect to Named routes; 4. Redirect to a controller or a controller with parameters; 5. Use session data to redirect, etc."/> back()" to redirect back to the previous page; 3. Redirect to Named routes; 4. Redirect to a controller or a controller with parameters; 5. Use session data to redirect, etc.">

Home  >  Article  >  PHP Framework  >  What are the methods for setting redirection in laravel?

What are the methods for setting redirection in laravel?

青灯夜游
青灯夜游Original
2021-09-24 14:34:403510browse

Methods to set redirection: 1. Use "Route::get()" and redirect() to redirect the URL; 2. Use "redirect()->back()" to redirect back to the previous URL. page; 3. Redirect to a named route; 4. Redirect to a controller or a controller with parameters; 5. Use session data redirection, etc.

What are the methods for setting redirection in laravel?

The operating environment of this tutorial: Windows 7 system, Laravel version 5, Dell G3 computer.

Several methods of redirection in Laravel

1. Redirect URL

Routing:

Route::get('itsolutionstuff/tags', 'HomeController@tags');

Controller:

public function home()
{
    return redirect('itsolutionstuff/tags');
}

2. Redirect back to the previous page

public function home()
{
    return back();
}
//或者
public function home2()
{
    return redirect()->back();
}

3. Redirect to the named route

Route:

Route::get('itsolutionstuff/tags', array('as'=> 'itsolutionstuff.tags', 'uses' => 'HomeController@tags'));

Controller:

public function home()
{
    return redirect()->route('itsolutionstuff.tags');
}

Redirect to named route using parameters

Route:

Route::get('itsolutionstuff/tag/{id}', array('as'=> 'itsolutionstuff.tag', 'uses' => 'HomeController@tags'));

Controller:

public function home()
{
    return redirect()->route('itsolutionstuff.tag',['id'=>17]);
}

4. Redirect to the controller

public function home()
{
    return redirect()->action('HomeController@home');
}

Redirect to the controller with parameters

public function home()
{
    return redirect()->action('App\Http\Controllers\HomeController@home',['id'=>17]);
}

5. Use session data redirection

We can also pass flashed session messages when redirecting with routing or url in the controller method, as shown below .

public function home()
{
    return redirect('home')->with('message', 'Welcome to PHP.cn!');
}

Related recommendations: The latest five Laravel video tutorials

The above is the detailed content of What are the methods for setting redirection in laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn