Problem with Parameters in Routing Path

I have this routing path set up

params: {}
path: "/people/:people_id?/:business_id?"

In this particular example, there is no 'people_id' to pass to the route and the 'business_id' = '532'. This is my code in Wappler

<button class="btn d-print-none btn-sm mb-2 btn-outline-success" dmx-on:click="browser1.goto('./people//'+business_ID_var.value)">

so when you click on the button the url looks like this (note the double forward slash which is where the 'people_id' would normally be)

/admin/people//532

And I would like to use 'params.business_id' at the other end but 'params.business_id' is not returning anything.

I can only imagine that I must have some value where the 'people_id' should be

image

Am I right?
Should it fail because of the way I have done it?
What should I put in the space between the two forward slashes?

Only parameters at the end are optional, you can’t have an optional parameter in the middle of a path.

In your path /people/:people_id?/:business_id? the 2 parameters are optional.
Valid:

/people
/people/1
/people/1/2

Invalid:

/people//2

So in this case the people_id is required when you want to pass a business_id.

1 Like

I don’t know if it will work in your case, but when I’ve come across this, I’ve inserted dummy/default values if there isn’t an actual value, using the ternary operator within the parameter expression. It solved the problem.

1 Like

Thanks @patrick for clarifying. I suspected that there would be something like that but couldn’t find any reference.

In your view Patrick, is it best to add a dummy value, or should I create two separate routes?
ie

path: "/people/:people_id?"
and
path: "/people/:business_id?"

Can I have as many routes as I want like this?

Thanks @TomD for your suggestion. What have you inserted?
Any of these type of things?

""
null
0
xxx
9999

Then this

image

should say “The last parameter can be suffixed with a…”

This is the same route.
It is seen as /people/:parameter_here? Also, you can't have two different routes pointing to the same page.

Thanks Teodor for clarifying. I feel much more comfortable with this moving forward :smile:

Where I've used it, the parameter is for display/SEO purposes rather than functional (eg query parameters), eg
/{{(author?author:'no author').stripTags(). etc

Nevertheless, I would have thought you could use any of the examples you listed.

1 Like

OK, I’ve just come to realise something about routes with parameters. If I have a route such as mentioned above

path: "/people/:people_id?"

and then I want to add an additional parameter like this

path: "/people/:people_id/:business_id?"

then I must go back to ALL occurrences that use this route path and add the additional parameter value even if it has to be a dummy value otherwise the links will fail to find the page and will return a 'status of 404 (Not Found)'

When defining a dynamic link href or the like, you can do something like:
for

path: "/people/:people_id/:business_id?"

the link could be

dmx-bind:href="'/people/'+peopleval.default('x')+'/'+businessval.default('x')"

I would imagine that you people and business IDs are both integers so setting a letter as the default ensures no match - you might just need to handle this on the page.

That way you could look up a person, a business or a person belonging to a business

1 Like

Cheers for that Ben :smile: