Detailed explanation of seven methods of redirection in Laravel

藏色散人
Release: 2023-04-05 14:12:01
Original
7756 people have browsed it

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.

Detailed explanation of seven methods of redirection in Laravel

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');
Copy after login

Controller:

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

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();
}
Copy after login

3. Redirect to the named route

The code example is as follows :

Route:

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

Controller:

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

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'));
Copy after login

Controller:

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

5. Redirect to the controller

The code example is as follows:

public function home()
{
    return redirect()->action('App\Http\Controllers\HomeController@home');
}
Copy after login

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]);
}
Copy after login

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!');
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!