The release notes for 2021.0.8 mentions the following feature:Improved: Route parameters [/{params:.}] for Custom Files*However, there’s no documentation on how to use it. I tried appending route parameters to a custom file URL, but the Route() function did not retrieve the parameters.For example:https://www.mydomain.com/MyCustomFile/data-for-route-1/data-for-route-2Both Route(1) and Route(2) were empty.How do I access the route parameters?
Client side:
$.get(ew.getApiUrl("MYFUNCTION") + '/' + Param1+ '/' + Param2+ '/' + Param3;
Server side (in Route_Action server event)
$app->get('/MYFUNCTION[/{params:.*}]', function ($request, $response, $args) {
$myParams = explode('/', $args['params']);
$p1 =$myParams['0'];
$p2 =$myParams['1'];
$p3 =$myParams['2'];
$response = $response->write($result);
return $response; // Return Psr\Http\Message\ResponseInterface object
});
}
sticcino, I appreciate your taking the time to post a response, but it doesn’t address my question.I know how to get parameters via the Route Action event, but that event creates a separate route, which has nothing to do with custom files. I’m asking specifically, how to get the parameters in the server-side events of a custom file. The Route() function can be used to access the parameters in normal pages and the release notes for 2021.0.8 state that custom files now support route parameters, which suggests the Route() function should work in custom files as well. If not, then what are the release notes refeferring to?Of course I can extract the parameters manually by splitting the request string, but I’m looking for a more elegant built-in approach as suggested by the release notes.
Since Custom File contains your own code, PHPMaker cannot generate a specific route for you, you do need to split the $args[‘params’] yourself, see Routing - Optional segments.
OK, but how do I access the request path from a page-level server event? I know I can just use $_SERVER[“REQUEST_URI”], but I would assume Slim provides a more elegant method to get just the request path.
You may use the PHPMaker function CurrentPageUrl().
That doesn’t help, because it only returns the base route, not including the parameters. It seems like there should be a function like GetArgs() or something that would return all the route parameters in an array.Currently, I’m using $path = explode(“/”, $_SERVER[“REQUEST_URI”]);, which splits the entire URL at the forward slashes. It works, but it’s a pretty ugly, inelegant solution, because you have to manually determine the array index of the first parameter, and it will vary depending on the number of segments in your base route, etc…
I’m aware of the Route() function, but when used in a custom file, it ignores the forward slash argument separator, so all the arguments appear in Route(1). For example:https://www.somedomain.com/my-route/arg1/arg2/arg3Calling the Route() function for this URL returns the following results:Route(1): arg1/arg2/arg3
Route(2): (empty)
Route(3): (empty)
That is because there is only one parameter in the route of Custom File (i.e.{params:.*}), you need to split the $args[‘params’] yourself. If you must define your own parameters, you may use the Route_Action server event (see Server Events and Client Scripts in the help file) and Slim’s setPattern() method to change the parameters, e.g. if the original route of the Custom File is: (see the generated src\routes.php)
$app->any('/myroute[/{params:.*}]', MyrouteController::class)->add(PermissionMiddleware::class)->setName('myroute-myroute-custom'); // custom
then you may try:
$app->getRouteCollector()->getNamedRoute('myroute-myroute-custom')->setPattern('/myroute[/{param1}/{param2}]'); // Change the pattern with named parameters
Then you can get each parameter by Route() or Route(“param”).
I understand now, thank you!