【问题标题】:can we connect Laravel Routing Parameter and Controller Parameter?我们可以连接 Laravel 路由参数和控制器参数吗?
【发布时间】:2019-06-30 05:44:17
【问题描述】:

所以我想填充键,但它在控制器内部填充另一个参数,我看到它会首先填充第一个参数。 反正有钥匙吗?会去 $key 参数和类型?到 $type 并填写?也要 $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); 
}

【问题讨论】:

    标签: laravel laravel-5 laravel-routing


    【解决方案1】:

    您可以替换 all() 和 type-hint 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); 
    }
    

    【讨论】:

    • 感谢您的解决方案,它就像一个魅力。我打算为每条路线制作功能:D
    【解决方案2】:

    当访问一个路由时,Laravel 从上到下遍历你的路由列表,直到找到一个“匹配”的路由,然后立即选择该路由。 所以, Route::get('/report/{id}',...Route::get('/report/all,...

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

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

    【讨论】:

    • OP 没有路由 report/{id}
    • 我知道,只是为了更好地理解问题。
    猜你喜欢
    • 2020-09-01
    • 2015-08-13
    • 1970-01-01
    • 2021-09-06
    • 2015-12-05
    • 2014-01-11
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多