Static routes in pimcore not working for more than one parameter - pimcore

I´d like to definie a static route in pimcore. This works find as long as the route does not have more than one parameter. As soon as I add a seconde one, I get the following error message:
You have requested a non-existent parameter "book/".
The resulting files looks like this:
pimcore:
staticroutes:
definitions:
f4a7a318-1d29-4f58-8a3c-d204d33a207a:
name: story
pattern: '/\/stories\/(.*)\/(.*)/'
reverse: /stories/%book/%title
controller: 'App\Controller\DefaultController::storiesAction'
variables: 'book,title'
defaults: null
siteId: { }
methods: null
priority: 0
creationDate: 1635722217
modificationDate: 1635722280
Any ideas?
Thanks in advance

I fiddled around with that a bit and found a solution. In previous versions I was able to use reverse patterns like this: /stories/%book/%title where book and title are the variables. However, it seems that the logic has changed here a bit. The following works now: /stories/%%book/%%title

Related

Passing AVP to prefix core function

I am working what appears to be a simple function for opensips 2.2.3, however cannot seem to get it working..
Essentially, extract the groupID from permissions module and add a prefix to R-URI on the egress side.
https://www.opensips.org/Documentation/Script-CoreFunctions-2-2#toc26
http://www.opensips.org/html/docs/modules/2.2.x/permissions.html#idp5689232
Config route looks like this:
route[relay] {
if ( get_source_group("$avp(group)") ) {
# do something with $avp(group)
xlog("group is $avp(group)\n");
};
#Add the string parameter in front of username in R-URI.
#prefix("$avp(group)");
#prefix("$avp(group){s.substr,0,0}");
$avp(22) = "3333#";
prefix("$avp(22)");
Prefix core function prefixes R-URI with variable name ($avp(22)) instead of value of "3333#".
I have tried various syntax versions that are commented out, however to no avail..
If I remove the quotes around the variable name:
prefix($avp(22));
Opensips does not startup at all, complaining about:
syntax error and bad argument, string expected
Am I missing something simple?
or
prefix function is simply not designed to work with variables?
Thank you in advance.
prefix() is somewhat old and unmaintained, hence it does not support variables. However, you can prepend your group to the R-URI username with:
$rU = $avp(group) + $rU;
xlog("My new R-URI is $ru. My new R-URI username is $rU\n");

validate.js returning error "unknown validator pattern"

I'm trying to use validate.js to validate input from the front end before entering it in the database with node but I'm getting an error that I can't figure out. I've gone over the docs and believe I setup the constraints correctly. The exact error is:
message:"Unknown validator pattern"
my validator is setup like this:
let alphanumeric = /^[a-zA-Z0-9]*$/;
let constraints = {
clientUsername:{
presence: true,
length: {min:8, max:15},
pattern:alphanumeric,
message: 'make sure client username is between 8-15 characters, is only numbers and letters'
},
tileCategory:{
presence:true,
length:{min:1, max:1},
numericality:{
onlyInteger:true,
lessThanOrEqualTo:tileCategoryNumber,
},
message:'enter a number, 1 char in length, less than or equal to 3' //the current number of tiles
}
};
validate({clientUsername: input.clientUsername},constraints);
At first I thought it was the regex pattern but tried commenting that out and then it said
message:"Unknown validator messsage"
so I'm guessing there is something wrong with my validator in general.
at the very top I of course included const validate = require('validate.js');
Something similar to this just burned me, have a look at the documentation again.
pattern is sort of a sub-validator of format and should look like:
{
format: {
pattern: "[A-Za-z0-9]+"
}
}
You're trying to use pattern at the "top level". I don't see anything in the documentation that implies helper patterns like alphanumeric exist. (I think the language the tool would use is to say "pattern is an option of the format validator" but I'm not sure.)
Your stated error message also implies a misspelling: it tells you it doesn't recognize messsage, which has 3 of the letter 's' but should have 2.
There's (2) things I could see being the issue. Firstly, you're using JS based regexes with the preceding and following /. Try removing these.
Beyond that, I'd recommend trying to remove the alphanumeric parameter & input the regex directly... it may be a type issue as well.
pattern:"^[a-zA-Z0-9]*$",
Hope this helps! :)

How to make optional params name in express route?

Here is below my code of route:-
app.get('/server/lead/get/:id?', leadCtrl.get);
app.get('/server/lead/filter/:filterQuery', leadCtrl.get);
As you see above i am using different route to access same controller method leadCtrl.get.
Now, i want something like route app.get('/server/lead/get/:id?:filter?', leadCtrl.get);. So, i can get params either req.params.id or req.params.filter but only one at a time.
What you asked in the question is not possible in the form that you describe it.
Now, i want something like route
app.get('/server/lead/get/:id?:filter?', leadCtrl.get);. So, i can get
params either req.params.id or req.params.filter but only one at a
time.
Your router would have no way to differentiate those two parameters. If it got a request to /server/lead/get/X then what is X? A filter or an ID?
Your options
You have few solutions here:
You can either keep using two routes like you did before.
You can use a common parameter for both cases as Robert explained in the comments.
Or you can use what seems to me the perfect solution for your use case - named query parameters - just use a route /server/lead/get and use query parameters to pass id and the filter.
Example URLs:
/server/lead/get?id=xxx
/server/lead/get?filterQuery=xxx
You will only have to make sure in your handler that only one of those two are set at a time with something like:
if (req.query.id && req.query.filterQuery) {
// respond with error
}
You can even mix the two if you have app.get('/server/lead/get/:id?') route you can have the id in the route and filterQuery as a query parameter. Now the URLs would be:
/server/lead/get/xxx (for id)
/server/lead/get?filterQuery=xxx (for filter)
For more info see: http://expressjs.com/en/api.html#req.query
Better way
If you follow some REST conventions then you can use:
app.get('/server/lead/:id') for one object with id (not optional)
app.get('/server/lead') for a list of objects (with optional filterQuery passed as a query parameter)
That way you would always know that when you access:
/server/lead/xxx - then it's one object with ID = xxx
/server/lead - then it's a list of any objects
/server/lead?filterQuery=xxx - then it's a list of objects that match the query
If you follow the REST conventions for things like this instead of inventing your own, it would be much easier for you to design the routes and handlers, and it would be much easier for other people to use your system.
You may also want to use plural /server/leads instead of /server/lead which is common with REST. That way it will be more obvious that leads is a list and leads/id is one of its elements.
For more info see:
https://en.wikipedia.org/wiki/Representational_state_transfer
http://www.restapitutorial.com/lessons/whatisrest.html
https://spring.io/understanding/REST
You have to realize that the following two routes match exactly the same:
app.get('/server/lead/get/:id?', leadCtrl.get);
app.get('/server/lead/get/:filter?', leadCtrl.get);
Express doesn't care about how you name the placeholders, so any requests for /server/lead/get/SOMEVALUE will always match the first (the one with :id).
You can add a distinction yourself, by only allowing a parameter to match a particular regular expression. From your code, it looks like :id should match MongoDB ObjectId's, so you can create a specific match for those:
app.get('/server/lead/get/:id([a-fA-F0-9]{24})?', leadCtrl.get);
If SOMEVALUE matches an ObjectId, it will call leadCtrl.get and populate req.params.id. If you also add another router for "the rest", you can also cover the req.params.filter case:
app.get('/server/lead/get/:filter?', leadCtrl.get);
As an aside: you're saying that you're passing JSON to the "filter" routes, in the URL. I would strongly suggest using a POST route for that, and post the JSON as request body content.

Swagger API "required" - how much is this required?

I've created a Swagger (on nodejs/express) test API with this specification (only relevant part):
...
parameters:
- name: name
in: query
required: true
type: string
...
But I can call the url with empty paramter, for example
http://localhost/test?name=
And it works without any problem, throws no exception or any other sign. Why?
If I make a similar call from the terminal via curl or via postman, it works as well. I parsed the query from the request object and found that in this case, the query parameter is interpreted as an empty string.
Making the call via SwaggerUI is different though, as the UI will actually not make the call UNLESS the query field has a value.
Try doing console.log(req.query); in your handler. You will probably see {name: ''}. Which is legitimate, just that the value of name is an empty string.
Look at JSON4 here: Representing null in JSON. So name IS defined, but it's empty.
You will probably need to do a check for empty string values.
I hope this helps!

How to use add_rewrite_rule function, while permalink structure is disabled?

I am using the add_rewrite_rule() function to modify my URL structure.
I'm wanting to use add_rewrite_rule to add a custom rule but these rules only get added in when other than default settings are selected in my permalink settings area.
i.e. in the settings there are following options:
- Default http://localhost/wordpress/?p=123
- Day and name http://localhost/wordpress/2014/08/14/sample-post/
- Month and name http://localhost/wordpress/2014/08/sample-post/
- Numeric http://localhost/wordpress/archives/123
- Post name http://localhost/wordpress/sample-post/
- Custom Structure http://localhost/wordpress
So, when I select other then 'Default', my add_rewrite_rule() function works, but while selecting 'Default', the function doesn't seem to be work. So please suggest me how to work the function in any condition. Any help would be Appriciated.
Update:
I think the problem lies here:
When I use this, while selecting 'Default':
get_option('permalink_structure');
I got nothing.
While in the other cases, there are some values like:
/%postname%/
/archives/%post_id%
/%year%/%monthnum%/%postname%/
The Default permalinks, or so called "Ugly" permalinks, are not adding anything to the .htaccess file, so the Apache rewrite engine is not enabled. Without the rewrite engine, no rewrites can be done. So the short answer is that rewrites are not possible with Default permalinks.
I can recommend you to use rewrites along with query vars. When adding a rewrite rule, pass your custom data to a query var, and build the functionality around that query var. This way your functionality will work in all situations and with all permalink types.
So for example if you have the following rule:
add_rewrite_rule('^sometest/([^/]*)/?','index.php?custom_query_var=$matches[1]', 'top');
and you have the custom_query_var added as a query var by using the following code:
function add_query_vars_filter( $vars ){
$vars[] = "custom_query_var";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
then when Default permalinks are selected, the following URL would work for you:
http://yoursite.com/index.php?custom_query_var=abc
and if "Pretty" permalinks are selected, the URL rewriting would work and your URL would look the following way:
http://yoursite.com/sometest/abc/
which is basically the best that can be achieved with rewrites.
I agree with #Martin. Here's a resource that will help https://core.trac.wordpress.org/ticket/15235
use this:
function my_add_query_vars( $qvars ) {
$qvars[] = 'business-coaching';
$qvars[] = 'country';
$qvars[] = 'territory';
$qvars[] = 'region';
return $qvars;
}
add_action('query_vars', 'my_add_query_vars');
//Write the rule
function add_analytic_rewrite_rule()
{
// Regex:The regex to match the incoming URL is:business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?
// Redirect Rule :The resulting internal URL: `index.php` because we still use WordPress
// `pagename` or page_id=45 because we use this WordPress page
// `country` : we will assign the first captured regex part to this variable
// `territory` we will assign the second captured regex part to this variable
// `region` we will assign the third captured regex part to this variable
add_rewrite_rule('business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?','index.php?page_id=45&country=$matches[2]&territory=$matches[`enter code `enter code here`here`4]&region=$matches[6]','top');//superfinal
}
add_action('init', 'add_analytic_rewrite_rule');

Resources