Is it possible to make an argument in a method controller to be optional?

falmp | 3 years ago

Currently I have:

/**
* !Route GET, $test
*/
function index($test = 'default') {}

Which doesn't work. If I try to access the controller with no parameters, I get a "Resource does not exist" exception.



falmp | 3 years ago

I have managed to do the optional arguments by looking closely to the routes tutorial. :)

My question below is still up though. Would it be possible to define a wildcard on the routes and then manage the following parameters using the Request resourceParts? Example:

!Route GET, /list/*

It seems the only available option is using the good old query strings.

reply

KrisJordan | 3 years ago

Great suggestion. I've come across one point where it would have been nice to be able to 'consume' the rest of a URL to a variable.

I agree this is interesting behavior. Not sure what the best syntax will be. We'll want to pass the remainder of the URL matched to the controller method.

Shouldn't complicate the routing mechanisms too much.

reply

Thomas | 3 years ago

Perhaps specifying a variable to be an array, and then the rest of the URL is split in chunks and put in that array. That could be a handy feature.

reply

falmp | 3 years ago

And while we are at it, what about arbitrary number of arguments?

I'm trying to build a grid with several optional filters, in the URL I need to pass the column name to sort, the order (ASC or DESC), number of results per page, page requested, column name of filter 1, column name of filter2, etc. Something like this:

/list/sort/$field/order/$order/number/$number/page/$page/filter1/$filter1/filter2/$filter2

It seems I don't have this kind of freedom: create a route /list/ and then just get the named parameters, using default values if not presented (order of parameters ideally would not matter too).

Is there a way or do I need to use another delimiter instead of / and parse the parameters myself?

reply

KevBurnsJr | 3 years ago

The URL ought to designate the resource, and the parameters ought to modify to the representation.

/groups/list?sort=field&order=desc&number=10
/groups/list?sort=field&order=desc&number=20

In each case you are asking for the same resource (a list of groups), but you are asking for a different representation of that resource (10 at a time or 20 at a time).

To me, this is the split between what belongs in the URL and what belongs in the parameters.

reply



Welcome KrisJordan!

Web: http://www.krisjordan.com/

Location: North Carolina

Edit Profile