A new function redirect() has been added to redirect()

Laravel5 in Laravel5 to replace Redirect::to() in Laravel4 for redirection. The function redirect() can redirect the user to a different page or action, and optionally choose whether to bring data for redirection. The

redirect response is an instance of the Illuminate\Http\RedirectResponse class that contains the necessary header information to redirect the user to another URL. The auxiliary function redirect returns an instance of the RedirectResponse class.

example routing:

Route::get('testRedirect', '[email protected]');

example action:

public function testRedirect()
    // 以下示例代码为此处实现

1|0Simple redirect

Suppose our current domain name is: http://localhost

return redirect('home');

redirect to http://localhost /home

return redirect('auth/login');

Redirect to http://localhost/auth/login

return redirect('');

to redirect to http://localhost, note that an empty string is passed in here, not nothing. It has been said above that redirect() returns an instance of the Illuminate\Http\RedirectResponse class.

2|0link method redirect

return redirect()->back();

redirect to the previous URL, you need to pay attention to the problem of infinite loop. The helper function back returns the previous URL (before using this method to ensure that the route uses the web middleware group or both use the session middleware). There is another way:

return back()->withInput();

3|0 redirect to the named route

// get('login', ['as' => 'userLogin', 'uses'=>'[email protected]']);
return redirect()->route('userLogin');

to redirect to the route named userLogin. This method is more flexible, because we will modify the URL structure in the future, just modify the configuration of the routes in routes.php.

// For a route with the following URI: profile/{id}
return redirect()->route('profile', [1]);

If there are parameters in the route, you can pass it as the second parameter to the route method.

return redirect()->route('profile', [$user]);

If you want to redirect to a route with an ID parameter (Eloquent model binding), you can pass the model itself and the ID will be automatically parsed.

4|0 Redirect to controller action

return redirect()->action('[email protected]');

Simply pass the controller and action name to the action method. Remember, you don't need to specify the full namespace of the controller, because Laravel's RouteServiceProvider will automatically set the default controller namespace.

return redirect()->action('[email protected]', [1]);

If the controller routes the parameters, you can pass the parameters as the second parameter to the action method.

5|0Redirecting with data

Redirecting to a new URL and storing the data into a one-time Session is usually done at the same time.

return redirect('dashboard')->with('status', 'Profile updated!');

This code will add the data to the Flash session flash data, and then you can use that data in the resulting Controller or view:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}

After the user redirects to the new page, you can remove the display from the Session One-time information.

Reminder: Session Flash Data Laravel has a concept of Session Flash data for only one request – after the next is loaded, that Session element/value is deleted, and can’t be retrieved again. This is exactly what is happening in with() method – its purpose is to add an error message or some input data only for that particular redirect and only for one page. It does the same thing as a function Session::flash(‘error’, ‘Something went wrong.’).

Laravel There is only one request session flash data concept. After the next request is loaded, the session data is deleted and cannot be retrieved again.

return redirect()->back()->withInput();

This method has no parameters. Its purpose is to save the old form values ​​to the Session. On the new page you can use old to get this data.