Let's say I have a route pointing to middleware;

Route::get("/user/{id}", ['middleware' => 'auth', function ($id) {

}]);

我的中间件代码如下:

public function handle($request, Closure $next)
{
    return $next($request);
}

如果我想在中间件中使用$id,我该怎么做?

推荐答案

You can use one of the following method to access the route parameter in a middleware:

First Method

$request->route()->parameters();

此方法将返回所有参数的array.

Second Method

$request->route('parameter_name');

Here parameter_name refers to what you called the parameter in the route.

Laravel相关问答推荐

如何更改LIVE上的Filament占位符内容?

在Laravel中URL中添加保护ID

验证判断请求的值是否存在于另一个表 Laravel 9 中

如何获取多态多对多关系子查询的自动 id 列名?

如何更改 laravel sanctum 返回message: Unauthenticated.

如何在 vue 模板中引用 laravel csrf 字段

Laravel/Eloquent:致命错误:在非对象上调用成员函数 connection()

使用 Amazon S3 的 Storage::get() 返回 false

在 Laravel Blade 中转义 vue.js 数据绑定语法?

Laravel - 超过 PHP 最大上传大小限制时验证文件大小

Auth 在 Laravel Tinker 中不起作用

在 Laravel 的 Homestead 中运行 PHPUnit

Laravel 初学者的良好开端

Laravel / Eloquent内存泄漏重复检索相同的记录

Laravel5如何将数组传递给查看

Select,where JSON 包含数组

Composer RuntimeException - 无法加载软件包 mews/purifier

Laravel 验证:数字和整数之间的区别?

Laravel 中的 isDirty() 是什么意思?

Laravel 4 控制器模板/Blade - 正确的方法?