In the Laravel framework, jumping to a page is a very common operation. Sometimes it is necessary to bring some parameters when jumping, such as search keywords, number of pages, etc. At this time, you need to implement the function of jumping to the page with parameters. This article will introduce how to implement the function of jumping pages with parameters and achieve parameter passing without redirection.
1. Basic jump
In the Laravel framework, there are two ways to jump, one is to use the redirect function, and the other is to use the view function. The redirect function is used to jump to other controller methods, and the view function is used to jump to the view file.
Using the redirect function, you can write like this:
return redirect()->action( '[控制器名称]@[方法名称]' );
Using the view function, you can write like this:
return view('[视图文件路径]', [参数数组]);
Both methods do not pass parameters. If you need to pass parameters, We need to take the following approach.
2. Jump with parameters
2.1. Use middleware
Laravel provides a very convenient middleware function. We can use middleware to pass parameters to the next Requesting.
We can create a new middleware, save the parameters in the middleware, and then retrieve these parameters in the next request. The middleware code is as follows:
<?php namespace App\Http\Middleware; use Closure; class ParamsMiddleware { public function handle($request, Closure $next) { $params = [ 'param1' => 'value1', 'param2' => 'value2', 'param3' => 'value3', ]; $request->session()->put('params', $params); return $next($request); } }
Save the parameters in the session and then take them out in the next request.
$params = $request->session()->get('params');
We can register the middleware in the web.php file and add the middleware to the route.
Route::middleware('params')->get('/', '[控制器名称]@[方法名称]');
The disadvantage of this method is that it requires the use of session. If the session is not turned on, it cannot be used.
2.2. Using URL parameters
We can also use URL parameters to pass parameters to the next page.
When jumping, we can use the route function to specify the route, and then set the parameters in the route.
Jump code:
return redirect()->route('[路由名称]', ['param1' => 'value1', 'param2' => 'value2']);
Routing code:
Route::get('/', '[控制器名称]@[方法名称]')->name('[路由名称]');
In the controller, we can use the request object to get the URL parameters.
$request->input('param1');
The disadvantage of this method is that the URL parameters will be displayed in the URL address bar, which may cause some trouble to users.
2.3. Using redirection parameters
Laravel provides another method to pass parameters without redirection. Let's look at the code first and then explain.
return redirect()->to('/')->with('param1', 'value1')->with('param2', 'value2');
When redirecting, we can use the with function to save the parameters in the session. This way, the parameters can be used in the next request.
In the controller, we can get the parameters like this.
$request->session()->get('param1'); $request->session()->get('param2');
This method achieves parameter passing without redirection, and also avoids the interference of URL parameters to users.
Summary
In Laravel, there are many ways to implement the function of jumping pages with parameters. You can use middleware, URL parameters or redirect parameters. Different methods have different advantages and disadvantages, and we need to choose the most appropriate method according to actual needs.
The above is the detailed content of How to implement the function of jumping page with parameters in laravel. For more information, please follow other related articles on the PHP Chinese website!