Home > PHP Framework > Laravel > body text

What are the methods for setting redirection in laravel?

青灯夜游
Release: 2023-01-13 00:40:30
Original
3486 people have browsed it

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

Controller:

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

2. Redirect back to the previous page

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

3. Redirect to the named route

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

Redirect to named route using parameters

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

4. Redirect to the controller

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

Redirect to the controller with parameters

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

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

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!

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!