且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5具有多个ID的路线

更新时间:2023-11-26 14:23:22

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'));
}

命名路线

我强烈建议您使用命名路由.

Named routes

I highly recommend using named routes.

Route::get('vault/{userId}/candidates/{candidateId}', [
    'as' => 'candidates.show', 
    'uses' => 'CandidateController@centreCandidatesShow'
]);

这不仅可以帮助您生成网址,还可以在传递参数时提供帮助!

This will not only help you generate url's but also helps when passing parameters!

示例:

<a href="{{ route('candidates.show', $userId, $candidateId) }}">Link to candidate</a>

这将提供链接并传递参数!

This will provide the link and pass in the parameters!

您甚至可以从控制器重定向到路由!

You can even redirect to a route from your controller!

return redirect()->route('candidates.show', $userId, $candidateId);

您可以将任意内容作为路由参数.大括号内的所有内容均视为有效参数.另一个示例是:

You can put whatver you want as route parameters. Anything inside curly brackets are considered a valid parameter. Another example would be:

Route::get('country/{country}/city/{city}/street/{street}/zip{zip}', 'AddressController@show');

在AddressController @ show中,您将按顺序接受这些参数.

In your AddressController@show you would accept these parameters, in order.

public function show($country, $city, $street, $zip) {..}

文档: http://laravel.com/docs/5.1/routing#route-参数