Home > Backend Development > PHP Tutorial > Detailed explanation of how to obtain routing parameters in Laravel

Detailed explanation of how to obtain routing parameters in Laravel

*文
Release: 2023-03-19 08:50:01
Original
2799 people have browsed it

This article mainly introduces you to the five methods of obtaining routing parameters Route Parameters in Laravel. The article introduces it in great detail through sample code. It has certain reference learning value for everyone to learn or use Laravel. Friends who need it can come below Let’s take a look. I hope to be helpful.

Preface

Everyone knows that Laravel has many ways to obtain routing parameters, and there are some pitfalls. They are summarized as follows and shared for your reference. Please refer to it for reference and study. I won’t say much more below. Let’s take a look at the detailed introduction.

Suppose we set a routing parameter:

/**
* 定义路由参数名称分别为: param1,param2
*/
Route::get('/{param1}/{param2}', 'TestController@index');
Copy after login

Now we visit http://test.dev/1/2

In TestController:

/**
* 路由参数获取方法
*
* @param Illuminate\Http\Request $request 依赖注入 Request 实例,放在参数中什么位置都可以自动加载
* @param mixed $arg2 要获取的路由参数
* @param mixed $arg1 要获取的路由参数
*/

public function index(Request $request, $arg2, $arg1)
{

 /**
 * 方法一:按照 URL 中路由参数先后顺序来获取
 * 注意:此种方式有个小坑,获取的值只与顺序有关,与名称无关
 */
 echo $arg2; //结果为 1 ,因为 $arg2 在第一位,获取的是第一个路由参数 param1 的值
 echo $arg1; //结果为 2 ,因为 $arg1 在第二位,获取的是第二个路由参数 param2 的值

 /**
 * 方法二:按照路由参数名称来获取
 * 注意:此处名称是 Route 中定义的参数名,非上面方法中的参数名 
 */
 $request->route('param1');  //结果为 1 ,获取的是第一个路由参数
 $request->route('param2');  //结果为 2 ,获取的是第二个路由参数

 /**
 * 方法三:使用 request() 辅助函数来获取,效果同方法二
 */
 request()->route('param1');  //结果为 1 ,如果不带路由参数名则返回当前的Route对象
 request()->route('param2');  //结果为 2 ,如果不带路由参数名则返回当前的Route对象

 /**
 * 方法四:使用 Route Facade
 */
 \Route::input('param1');  //结果为 1 ,该方法必须带路由参数名
 \Route::input('param2');  //结果为 2 ,该方法必须带路由参数名

 /**
 * 方法五:使用 Illuminate\Http\Request 实例动态属性
 */
 $request->param1; //结果为 1 ,Laravel 5.4+ 可用
 $request->param2; //结果为 2 ,Laravel 5.4+ 可用
  
 // 或者
 request()->param1; //结果为 1 ,Laravel 5.4+ 可用
 request()->param2; //结果为 2 ,Laravel 5.4+ 可用
  
 //或者
 request('param1'); //结果为 1 ,Laravel 5.4+ 可用
 request('param2'); //结果为 2 ,Laravel 5.4+ 可用
  
 /**
 * 注意:Laravel 在处理动态属性的优先级是,先从请求的数据(POST/GET)中查找,没有的话再到路由参数中找。
 * 例如:URL : http://test.dev/1/2?param1=a&param2=b
 * $request->param1; request()->param1; request('param1'); //结果为 a
 * $request->param2; request()->param2; request('param2'); //结果为 b
 */
}
Copy after login

The above are the 5 methods for Laravel to obtain routing parameters.

Related recommendations:

Detailed explanation of how to add custom classes to the IoC container in Laravel5.4

Detailed explanation of how Laravel5.4 implements multi-field login

##Detailed explanation of how Laravel optimizes Model queries through preloading

The above is the detailed content of Detailed explanation of how to obtain routing parameters 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