参数不来自URL的laravel路由 [英] laravel route with parameter not coming from url

查看:153
本文介绍了参数不来自URL的laravel路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几条路线:

Route::get('pending-submit', 'CasesController@cases');
Route::get('submited', 'CasesController@cases');
Route::get('closed', 'CasesController@cases');

即使在路由器API文档中,我也一直在四处查看,除了在控制器内创建多种方法外,我找不到满足我要求的解决方案.该方法执行完全相同的查询,除了添加where子句以标识每种情况之间的不同状态外,我试图做的就是拥有这样的方法

I have been looking around even in the router API documentation and I can't find a solution for my requirement other than creating multiple methods within the controller. The method does the exact same query except for adding a where clause to identify the different status between each case, what I was trying to do is have a method like this

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

而不是这样做:

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

但是我可以找到一种从路由将参数传递给方法的方法,因此我现在必须为每个路由创建一个对我来说似乎不必要的方法.我知道我可以只生成带有get参数的url,但是我想使它更整洁,有没有办法我可以在不将其包含在url中的情况下添加该参数?

But I can figure a way to pass that parameter to the method from the route so I now have to create a method for each route which does not seem necessary to me. I understand I could just generate urls with the get parameter in it but I wanted to make that cleaner, is there a way for me to add that parameter without having it in the url?

顺便说一句,我也尝试了这样的事情,但没醒:

By the way, I also tried something like this which did not wok:

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



我知道我可以创建类似 https://someurl.com/cases?status=0,也可以包含 https://someurl.com/cases 这样的URL,但是每个路由都需要使用不同的方法我想要的是具有 https://someurl.com/cases 之类的URL,并且有一个方法,其中的参数是由路由器传递,而不是从请求中获取,所以我可以这样:

I understand I can make URLs like https://someurl.com/cases?status=0 and can also have URLs like https://someurl.com/cases which require a different method per route however what I want is have URLs like https://someurl.com/cases and have a single method where the parameter is passed by the router instead of me getting it from the request so I can do it like this:

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 */
}



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

@AndyNoelker what I have is 3 different values either 0, 1 or 2

我想要这样的东西

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

如果不能从routes.php中获取它,那很好,我只想知道,您提供给我的所有其他方法都不是我想要或要求的,因为我已经知道该怎么做.

If not possible from the routes.php it is fine, I just want to know, all other methods you are giving me is not what I want or asking for since I already know how to do those.

推荐答案

您将必须通过URL传递所需的状态-否则,该路线将无法知道您所需的状态.您可以通过URL查询参数或作为完整的route参数来完成此操作.在这种情况下,我个人建议使用查询参数,但我将向您展示两者.

You are going to have to pass the desired status in through the URL - otherwise the route will have no way of knowing which status you desire. You can either do it through URL query parameters or as a fully-fledged route parameter. I would personally suggest using a query parameter in this case, but I'll show you both.

URL

example.com/cases?status=1

路线

Route::get('cases', CasesController@cases);

CasesController

CasesController

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

    return Cases::where('status',$status)->get();
}

使用路线参数

URL

Using Route parameters

URL

example.com/cases/1

路线

Route::get('cases/{id}', CasesController@cases);

CasesController

CasesController

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

当然,如果您希望他们在路由中使用a或唯一ID以外的其他东西,则必须在查询中对此进行调整,但这应该给您正确的想法.

Of course if you'd prefer that they use a slug or something other than a unique id in the route, then you'd have to adjust for that in your query, but this should give you the right idea.

这篇关于参数不来自URL的laravel路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆