Handling routes with empty parameters

I am using a route with three dynamic parameters. The first is functional (the record id); the other two are for SEO purposes. One of the last two might have no value, resulting in the query string appearing like this:
/item/4868//scott-walter
… with a double forward slash. In this case the query fails. Is there an option to allow for empty parameters? I thought the ? suffix might do this, but I think this must mean the parameter itself is optional, not whether or not it contains a value.

If there isn’t an option for this, I’ll set a default to avoid empty parameters. I tried using:
{{author.default('no author')}}
… but this doesn’t return anything.

I used:
{{author?author:'no author'}}
… and it worked as expected.

There seems to be a problem using the ‘default’ formatter, unless I’m not using it correctly. (This is in App Connect; I haven’t had any problems using ‘default’ in Server Connect, in query builder.)

Hello Tom,
Have you tried using the conditions in the query builder - where you define the filters?
So that the filter is only applied when it returns a value?

Hi Teodor,

The query builder in server connect? I’m not sure what you mean. I could use this to return a default value for the purposes of routing, but I’m using the query results on the page and don’t want a default value to appear.

I don’t see how I could use query builder to prevent an empy parameter being sent. If the route is set up with three parameters, then there will be three expression fields in the routes properties panel, in app connect. Whatever values are returned by the expressions, they will be separated by a forward slash.

Of course there are ways round it - eg by using a default for otherwise empty parameters (which is what I’ve done). There is no need to use the routes panel (in app connect) at all to create the expression, in which case you could prevent empty parameter being included but I wanted a method which would use the panel.

As it is, the routes panel is not very useful because the expressions do not repopulate the parameter fields if you want to edit them - but I imagine this is a bug which will be fixed. Once it’s fixed it would be useful to be able to use the panel, particularly for more complicated expressions.

Tom, isn’t your issue that a parameter which filters a database query is missing a value ?
If yes, then the conditions in the server connect query builder is what you need to use…
So when a query parameters has no value - don’t apply the filter.

I probably didn’t explain things very well and made things more complicated than necessary.

I have a page showing a list of items (the result of a query). Previously I had a simple link to a detail page, eg with a query string:
detail.php?id=1243
… which will of course reliably display a single record (there is no problem with query; it will never fail).

I’m using the same underlying query but adding 2 parameters, containing data from the record. So the route looks like this:
/item/:id/:author/:title
The only issue is that if a record doesn’t have an author (in this example), the query will contain an empty parameter and will fail (although the functional part - the ID - will never be empty of course). Eg
/item/4868//scott-walter
…fails (doesn’t return record 4868, whereas:
/item/4868/no-author/scott-walter
… does return the record.

I wondered if there was a way of setting up a route so that empty parameters are ignored rather than causing a query like this to fail.

So you are not actually using the two parameters to filter the query?

No - they’re just for the way the URL is displayed/ SEO purposes.

I am afraid there is no way to exclude parameters, which you already defined in the routing options. The routing component expects the value as you defined it.

I appreciate the routing component expects the parameter structure as you define it* - but I assumed it would have no issue with the actual values (even if they are empty). I see now this is not the case and an empty value causes a link to fail (which I didn't expect). Perhaps it's an htaccess issue. In any case, it's not a problem now I'm aware of it.

*Actually, it seems there is some flexibility and initially I assumed this modifier would solve the problem:

image
.. but as far as I can see there is no way to send parameters conditionally, at least not using the Wappler UI.

Anyway, it's not very important and I've found a solution.

So what happens when you add the ? after the query parameter?

If you add the ?, the parameter is optional, so:
/item/4868/scott-walter
… will work, whereas:
/item/4868//scott-walter
… will fail.

Using the route parameters panel in app connect, you can’t control the number of parameters which are included in the URL, so you could only make use of this feature by entering the code manually - but it could be a useful feature. However when you are adding a series of formatters to each parameter (stripTags, truncate, slugify etc.), the UI is very helpful.