routes.php:
Route::get('vault/{userId}/candidates/{candidateId}', 'CandidateController@centreCandidatesShow');
CandidatesController.php:
public function centreCandidatesShow($userId, $canidateId)
{
$candidate = Candidate::with('qualification')->find($canidateId);
$user = User::find($userId);
return view('vault.show', compact('candidate'));
}
命名路线
我强烈推荐使用命名路由。
Route::get('vault/{userId}/candidates/{candidateId}', [
'as' => 'candidates.show',
'uses' => 'CandidateController@centreCandidatesShow'
]);
这不仅可以帮助您生成 url,还可以帮助您传递参数!
例子:
<a href="{{ route('candidates.show', $userId, $candidateId) }}">Link to candidate</a>
这将提供链接并传入参数!
您甚至可以从控制器重定向到路由!
return redirect()->route('candidates.show', $userId, $candidateId);
说明:
你可以把任何你想要的作为路由参数。大括号内的任何内容都被视为有效参数。另一个例子是:
Route::get('country/{country}/city/{city}/street/{street}/zip{zip}', 'AddressController@show');
在您的 AddressController@show 中,您将按顺序接受这些参数。
public function show($country, $city, $street, $zip) {..}
文档:http://laravel.com/docs/5.1/routing#route-parameters