Pathinfo Providers
By default, the URL mapping processor will detect routes by looking at the CGI.PATH_INFO variable, but you can override this and provide your own function. This feature can be useful to set flags for each request based on a URL and then clean or parse the URL to a more generic form to allow for simple route declarations. Uses may include internationalization (i18n) and supporting multiple experiences based on devices such as Desktop, Tablet, Mobile and TV.
To modify the URI used by the Routing Services before route detection occurs simply follow the convention of adding a function called pathInfoProvider() to your application Router (config/Router.cfc).
The pathInfoProvider() function is responsible for returning the string used to match a route.
1
// Example PathInfoProvider for detecting a mobile request
2
function PathInfoProvider( event ){
3
var rc = event.getCollection();
4
var prc = event.getCollection(private=true);
5
6
local.URI = CGI.PATH_INFO;
7
8
if (reFindNoCase('^/m',local.URI) == 0)
9
{
10
// Does not look like this could be a mobile request...
11
return local.URI;
12
}
13
14
// Mobile Request? Let's find out.
15
16
// If the URI is "/m" it is easy to determine that this is a
17
// request for the Mobile Homepage.
18
if (len(local.URI) == 2)
19
{
20
prc.mobile = true;
21
// Simply return "/" since they want the mobile homepage
22
return "/";
23
}
24
25
// Only continue with our mobile evaluation if we have a slash after
26
// our "/m". Without a slash following the /m the route is something
27
// else like coldbox.org/makes/cool/stuff
28
if (REFindNoCase('^/m/',local.URI) == 1)
29
{
30
// Looks like we are mobile!
31
prc.mobile = true;
32
33
// Remove our "/m/" determination and continue
34
// processing for languages...
35
local.URI = REReplaceNoCase(local.URI,'^/m/','/');
36
}
37
38
// The URI starts with an "m" but does not look like
39
// a mobile request. So, simply return the URI for normal
40
// route detection...
41
return local.URI;
42
}
Copied!
The Rewrite rules section has another useful example for a pathInfo provider
Last modified 9mo ago
Copy link