我们可以连接 Laravel 路由参数和控制器参数吗?

can we connect Laravel Routing Parameter and Controller Parameter?

所以我想填充键,但它在控制器内部填充了另一个参数,我看到它会先填充第一个参数。 有办法制作钥匙吗?将转到 $key 参数和类型? $键入并填写?也要 $fill 吗?

我正在使用 Laravel 5.6。*

Route::get('/report', 'ReportAPIController@index')->name('api.report');
Route::get('/report/all', 'ReportAPIController@all')->name('api.report.all');
Route::get('/report/all/key/{key?}', 'ReportAPIController@all')->name('api.report.all.key');
Route::get('/report/all/search/{type?}/{fill?}', 'ReportAPIController@all')->name('api.report.all.type.fill');
Route::get('/report/all/search/{type?}/{fill?}/key/{key?}', 'ReportAPIController@all')->name('api.report.all.type.fill.key');

预期结果:空空测试 /report/all/key/testing

public function all($type = null,$fill = null,$key = null)
{
    dd($type.$fill.$key); 
}

实际结果:测试 null null /report/all/key/testing

public function all($type = null,$fill = null,$key = null)
{
    dd($type.$fill.$key); 
}

您可以替换 all() 中的参数和类型提示 Illuminate\Http\Request

那么,你可以这样做:

use Illuminate\Http\Request; 

public function all(Request $request)
{
    $key  = $request->key;
    $type = $request->type;
    $fill = $request->fill;

    dd($type.$fill.$key); 
}

访问一条路线时,Laravel 从上到下遍历您的路线列表,直到找到 'matches' 一条路线,然后立即选择这条路线。 所以, Route::get('/report/{id}',...Route::get('/report/all,...

对于请求类型 /report/all 将匹配 /report/{id},因为它是第一个 MATCH。

在你的情况下,你必须恢复你的路线顺序,所以最难实现的路线将排在第一位。