return redirect ()->route ('users.show', [$user]); 路由调用时该怎么传入参数?
看源码,路由调用时,参数应该传入数组:
public function route($route, $parameters = [], $status = 302, $headers = [])
{
return $this->to($this->generator->route($route, $parameters), $status, $headers);
}
官方文档中给了个例子,
Route::get('user/{id}/profile', function ($id) {
//
})->name('profile');
$url = route('profile', ['id' => 1]);
标题中的语句,参数写成了 [$user],这种写法不太理解。简写的话不应该是 compact($user) 吗?
而在 _header.blade.php 中,则完全没有使用数组,只写了值:<a href="{{ route('users.show', Auth::user()->id) }}">个人中心</a>
经测试,这几种写法效果一样。这是 PHP 的语言特性吗?有点迷惑
推荐文章: