参数不是来自url的laravel路由

Tla*_*uis -1 php routing laravel-5.1

我有多个看起来像这样的路线:

Route::get('pending-submit', 'CasesController@cases');
Route::get('submited', 'CasesController@cases');
Route::get('closed', 'CasesController@cases');
Run Code Online (Sandbox Code Playgroud)

即使在路由器 API 文档中,我也一直在环顾四周,除了在控制器中创建多个方法之外,我找不到满足我的要求的解决方案。该方法执行完全相同的查询,除了添加一个 where 子句来标识每个案例之间的不同状态,我试图做的是有一个这样的方法

public function cases($whereStatus = 0){
    return Cases::where('status', $whereStatus)->get();
}
Run Code Online (Sandbox Code Playgroud)

而不是这样做:

public function pendingCases(){
    return Cases::where('status', 0)->get();
}

public function submitedCases(){
    return Cases::where('status', 1)->get();
}

public function closedCases(){
    return Cases::where('status', 2)->get();
}
Run Code Online (Sandbox Code Playgroud)

但是我可以想办法将该参数从路由传递给方法,所以我现在必须为每个路由创建一个对我来说似乎没有必要的方法。我知道我可以只生成带有 get 参数的 url,但我想让它更简洁,有没有办法让我添加该参数而不在 url 中添加它?

顺便说一句,我也尝试过这样的事情,但没有成功:

Route::get(
    'pending-submit',
    array(
        'uses'   => 'CasesController@cases',
        'params' => array(
            'filter' => 0
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

编辑:

我知道我可以创建像https://someurl.com/cases?status=0这样的 URL,也可以像https://someurl.com/cases这样的URL需要不同的方法,但是我想要的是像这样的 URL https://someurl.com/cases并有一个方法,其中参数由路由器传递,而不是我从请求中获取,因此我可以这样做:

public function myMethod($param){
    /*
     * Here I access the $param without calling Request::input('someparam');
     * or without Request::path() where then I have to check what path is it
    */
    echo $param; /* this should already have the param from the route */
}
Run Code Online (Sandbox Code Playgroud)

编辑:

@AndyNoelker 我拥有的是 3 个不同的值,0、1 或 2

我想要这样的东西

Route::get(
    'cases',
    array(
        'uses'   => 'CasesController@cases',
        'status' => 0 /* this is what I need */
    )
);
Run Code Online (Sandbox Code Playgroud)

如果从 routes.php 不可能,那很好,我只是想知道,你给我的所有其他方法都不是我想要或要求的,因为我已经知道如何做这些了。

And*_*ker 5

您将不得不通过 URL 传递所需的状态 - 否则路由将无法知道您想要哪种状态。您可以通过 URL 查询参数或作为成熟的路由参数来完成。我个人建议在这种情况下使用查询参数,但我会向您展示两者。

使用查询参数

网址

example.com/cases?status=1
Run Code Online (Sandbox Code Playgroud)

路线

Route::get('cases', CasesController@cases);
Run Code Online (Sandbox Code Playgroud)

案例控制器

public method cases(Request $request)
{
    $input = $request->all();
    $status = $input['status'];

    return Cases::where('status',$status)->get();
}
Run Code Online (Sandbox Code Playgroud)

使用路由参数

网址

example.com/cases/1
Run Code Online (Sandbox Code Playgroud)

路线

Route::get('cases/{id}', CasesController@cases);
Run Code Online (Sandbox Code Playgroud)

案例控制器

public method cases($id)
{
    return Cases::where('status',$id)->get();
}
Run Code Online (Sandbox Code Playgroud)

当然,如果您希望他们在路线中使用 slug 或其他东西而不是唯一 id,那么您必须在查询中对此进行调整,但这应该会给您正确的想法。