This is a small component that does a simple regex check of the user agent string, then routes to the specified mobile url. There are parameters you can set in the send() function.
Last active
March 5, 2017 17:44
-
-
Save stevereich/3eed22db327ad6b62488e06f73e21c6b to your computer and use it in GitHub Desktop.
This is a component that will do simple regex check and seamlessly route user to an alternate mobile url. URL parameters can be passed to force back and forth and return to the default view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Mobile Router Demo (Mobile Page)</title> | |
</head> | |
<body> | |
<p>mobile site</p> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// can be used page by page | |
local.mobileRoute = createobject('component','router').init(); | |
local.mobileRoute.sendMobile('_mobile.cfm'); | |
// or if used globally, set in application.cfc | |
// application.mobileRoute = createobject('component','router').init('_mobile.cfm'); | |
// then call... | |
// application.mobileRoute.sendMobile(); | |
</cfscript> | |
<html> | |
<head> | |
<title>Mobile Router Demo (Desktop Site)</title> | |
</head> | |
<body> | |
<p>desktop site</p> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component output=true { | |
public router function init(string mobileURL){ | |
variables.mobileURL = (structkeyexists(arguments, 'mobileURL') && len(arguments.mobileURL) && fileexists(arguments.mobileURL)) ? arguments.mobileURL : ''; | |
return this; | |
} | |
public any function sendMobile(any params, struct url, struct session, struct cgi) output=true { | |
// make copies of required server vars | |
arguments.url = (structkeyexists(arguments,'url')) ? arguments.url : structcopy(url); | |
arguments.session = (structkeyexists(arguments,'session')) ? arguments.session : structcopy(session); | |
arguments.cgi = (structkeyexists(arguments,'cgi')) ? arguments.cgi : structcopy(cgi); | |
///////////////////////////////////////// | |
// EXAMPLE: ///////////////////////////// | |
///////////////////////////////////////// | |
// | |
// sendMobile({ | |
// mobileURL = 'mobile.cfm', | |
// doRedirect = false, | |
// appendQueryString = true, | |
// logErrors = true, | |
// namespace = 'myMobileVars', | |
// mobileCriteria = ['mobile'] | |
// }); | |
// | |
// -or- | |
// | |
// sendMobile('mobile.cfm') | |
// | |
///////////////////////////////////////// | |
var options = (structkeyexists(arguments,'params') && isStruct(arguments.params)) | |
? arguments.params | |
: (structkeyexists(arguments,'params') && isValid('string',arguments.params) && len(arguments.params)) | |
? {'mobileURL'=arguments.params} | |
: {}; | |
// set defaults for all vars (error handling) | |
// url for mobile site | |
var mobileURL = (structkeyexists(options,'mobileURL') && isValid('string',options.mobileURL) && len(options.mobileURL) && fileexists(expandpath(options.mobileURL))) | |
? options.mobileURL | |
: len(variables.mobileURL) | |
? variables.mobileURL | |
: arguments.cgi.script_name; | |
// whether or not to redirect or just include page (default is false) | |
var doRedirect = (structkeyexists(options,'doRedirect') && isBoolean(options.doRedirect)) | |
? options.doRedirect | |
: false; | |
// whether to append query string to mobile url. only applies if doRedirect is true (default is true) | |
var appendQueryString = (structkeyexists(options,'appendQueryString') && isBoolean(options.appendQueryString)) | |
? options.appendQueryString | |
: true; | |
// add query string to URL if appendQueryString is true | |
mobileURL = (appendQueryString && doRedirect && len(arguments.cgi.query_string)) | |
? mobileURL & '?' & arguments.cgi.query_string | |
: mobileURL; | |
// whether or not to log script errors in this page to the dev console (default is false) | |
var logErrors = (structkeyexists(options,'logErrors') && isBoolean(options.logErrors)) | |
? options.logErrors | |
: true; | |
// namespace session variable to avoid conflicts with existing var structure | |
var namespace = (structkeyexists(options,'namespace') && isValid('string',options.namespace) && len(options.namespace)) | |
? options.namespace | |
: 'mobileRouter'; | |
// this doesn't need to be changed unless you want to search for specific | |
// criteria or more detailed user agent string info. these are strings to | |
// search for in agent string to indicate mobile device | |
var mobileCriteria = (structkeyexists(options,'mobileCriteria') && isArray(options.mobileCriteria) && arraylen(options.mobileCriteria)) | |
? options.mobileCriteria | |
: ["ip(ad|hone)","android","blackberry","mobile","iemobile","windows phone"]; | |
try { | |
// only run this script if the mobile url is a different page from this one | |
if (trim(expandpath(mobileURL)) != trim(expandpath(arguments.cgi.script_name))) { | |
// define namespaced session structure | |
session[namespace] = (structkeyexists(arguments.session,namespace)) ? arguments.session[namespace] : {}; | |
// set this variable according to user's agent string. this will | |
// be the default if no current view or manually set view | |
var isMobile = (refindnocase(arraytolist(mobileCriteria,'|'),arguments.cgi.http_user_agent) > 0) ? true : false; | |
// set doMobile either with previously set current | |
// view, through search of agent string, or manunally set | |
// with url parameter | |
var doMobile = (structkeyexists(arguments.session[namespace],'mobile')) | |
? session[namespace].mobile | |
: isMobile; | |
// check for manually setting mobile view (?mobileView) | |
doMobile = (structkeyexists(arguments.url,'mobileView')) | |
? true | |
: doMobile; | |
// check for manually setting desktop view (?desktopView) | |
doMobile = (structkeyexists(arguments.url,'desktopView')) | |
? false | |
: doMobile; | |
// check for manually setting default view (reset any | |
// previously set view -- ?defaultView) | |
doMobile = (structkeyexists(arguments.url,'defaultView')) | |
? isMobile | |
: doMobile; | |
// include mobile page based on requested view, set session of | |
// current view, and don't let request continue through. | |
if(doMobile){ | |
session[namespace].mobile = true; | |
if(doRedirect){ | |
location url=mobileURL, addtoken="false"; | |
} | |
else{ | |
include mobileURL; | |
abort; | |
} | |
} | |
else{ | |
session[namespace].mobile = false; | |
} | |
} | |
} | |
catch(any error){ | |
// output any error that happens above to the dev console | |
if(logErrors){ | |
writeoutput('<script type="text/javascript">console.log("%cCOLFUSION ERROR\n%cTEMPLATE: '& replacenocase(GetCurrentTemplatePath(),"\","\\","ALL") &'\n\n%c'& error.message & '","font-size:18pt;color:##C00;font-weight:bold","","background-color:##FF6;font-weight:bold;color:##000")</script>'); | |
}; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment