In this article, I will introduce to you how to redirect users from one page to another page from the controller. We usually use the redirect() method to redirect the user in the controller.
Laravel 5 version provides redirect(), then we can simply use redirect() in Laravel 5.0, Laravel 5.1, Laravel 5.2 and Laravel 5.3.
Below we will introduce to you seven methods of redirection in Laravel.
1) Redirect to the URL
2) Redirect back to the previous page
3) Redirect to the specified route
4) Redirect to the specified route with parameters
5) Redirect to the controller
6) Redirect to the controller with parameters
7) Redirect using session data
1. Redirect URL
In the following example, I simply redirect the URL of "itsolutionstuff/tags".
Route:
Route::get('itsolutionstuff/tags', 'HomeController@tags');
Controller:
public function home() { return redirect('itsolutionstuff/tags'); }
2. Redirect back to the previous page
In this example, We can redirect back to the URL of our previous page, so you can use two methods:
public function home() { return back(); } //或者 public function home2() { return redirect()->back(); }
3. Redirect to the named route
The code example is as follows :
Route:
Route::get('itsolutionstuff/tags', array('as'=> 'itsolutionstuff.tags', 'uses' => 'HomeController@tags'));
Controller:
public function home() { return redirect()->route('itsolutionstuff.tags'); }
4. Use parameters to redirect to the named route
The code example is as follows :
Route:
Route::get('itsolutionstuff/tag/{id}', array('as'=> 'itsolutionstuff.tag', 'uses' => 'HomeController@tags'));
Controller:
public function home() { return redirect()->route('itsolutionstuff.tag',['id'=>17]); }
5. Redirect to the controller
The code example is as follows:
public function home() { return redirect()->action('App\Http\Controllers\HomeController@home'); }
6. Redirect to a controller with parameters
The code example is as follows:
public function home() { return redirect()->action('App\Http\Controllers\HomeController@home',['id'=>17]); }
7. Redirect using session data
We can also pass flashed session messages when redirecting with route or url in controller method as shown below.
public function home() { return redirect('home')->with('message', 'Welcome to PHP.cn!'); }
Related laravel video tutorials: "Latest Laravel Mall Practical Video Tutorial"
This article is about the various methods of using redirect() to redirect URLs in Laravel 5 Introduction, I hope it will be helpful to friends in need!
The above is the detailed content of Detailed explanation of seven methods of redirection in Laravel. For more information, please follow other related articles on the PHP Chinese website!