假设我在我的路由中定义了一个资源:
Route::resource('account', 'AccountController', ['only'=> ['index','update']]);
然后我将 Middleware
从内部附加到 Controller
:
public function __construct() {
$this->middleware('BeforeAccount', ['only' => ['update']]);
}
假设我想在我的中间件中访问帐户之后出现的 uri 参数(即 example.com/account/2
)——我该如何获取该变量?
最佳答案
您可以使用以下代码来实现:
public function handle($request, Closure $next)
{
$account_id = $request->route()->parameter('accounts');
//...
}
由于 handle
方法接收 Request
对象作为第一个参数。 middleware
仅在路由匹配后执行,因此 Request
对象包含当前路由,无需使用 Route::getRoutes( )->匹配($request)
。
关于php - Laravel 5 - 在资源中间件中获取 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712218/