mercredi 27 juin 2018

How to use optional route parameters properly with Laravel 5.6?

I am trying to create an API with Laravel 5.6, however, it seems to me that it's not possible to use optional route parameters before/after the parameter.

I'd like to achieve the following:

Route::get('api/lists/{id?}/items', 
[
    'as'    => 'api/lists/items/get', 
    'uses'  => 'ListsController@getListItems'
]);

With the above scenario, if I'm trying to visit api/lists/1/items it shows the page. On the other hand, if I'm trying to visit api/lists/items it says that the page is not found.

What I basically want is if there's no List ID specified, Laravel should fetch all the List ID's items, otherwise it should only fetch the specific ID's items.

Q: How is it possible to the optional parameter in between the 'route words'? Is it even possible? Or is there an alternative solution to this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire