简体   繁体   中英

laravel route with parameter not coming from url

I have multiple routes that look like this:

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

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

Instead of doing this:

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

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

EDIT:

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

EDIT:

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

I want to have something like this

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

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.

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.

Using Query parameters

URL

example.com/cases?status=1

Routes

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

CasesController

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

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

Using Route parameters

URL

example.com/cases/1

Routes

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

CasesController

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

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.

I think you're looking for route parameters:

Route::get("/cases/{case}", "CasesController@cases");

This will match any of the following:

some_url/cases/pending-submit
some_url/cases/submited
some_url/cases/closed
...

Then, your function public function cases in CasesController would look like this:

public function cases($case){
    if($case == "pending-submit"){
      // Do Something
    } else if($case == "submited") {
      // Do Something Else
    }

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

Etc etc. Look more into url parameters here: Documentation

  Route::get('pending-submit', array('status' => 0, 'uses' => function(){
   $CasesController = $app->make('CasesController');
   return $CasesController->callAction('cases', $parameters = array());
 }));

If I am understanding your question correctly, this should work.

Web Route

Route::get('pending-submit', [
    'as'        => 'pending',
    'uses'      => 'CasesController@cases',
    'status-id' => '0'
]);

And, you can access the parameter passed with the route like below,

In Controller

$request->route()->getActions('status-id')

In View

{{ request()->route()->getAction('status-id') }}

Tested and perfectly works in L5.6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM