Hoping to get some help with runtime route loading...
# cfwheels
a
Hoping to get some help with runtime route loading. I'm on CFWheels 1.4.5 I sling this in my config/routes.cfm file:
Copy code
addRoute(
	name="myRoute",
	pattern="myPattern",
	controller="myController",
	action="myAction"
)
If I then reload the app, I can then browse to http://localhost/myPattern/myAction, and it runs. Cool. However if I try to do this on the fly, eg:
Copy code
addRoute(
	name="myRoute",
	pattern="myPattern",
	controller="myController",
	action="myAction"
)
http method="get" url="<http://localhost/myPattern/myAction>";
Then the request is handled by the
/
route. If I dump out
application.wheels.routes
immediately after the
addRoute
call, I see
myRoute
in the array. Equally, if I dump it out in the
/
route view, then I also see it there. So I'm wondering what the missing piece of the puzzle is that I need to do so that it's actually... used. It's like I've set it up, but I've not loaded it or something?
p
@Adam Cameron sorry I'm traveling and could reply sooner. It seems you are asking about the caching of the routes. So if you add a new route via code and reload it works but if you add a route and don't reload it doesn't work even though you see it in the array. There may be a way to re-cache just the routes without reloading the entire app.
Try calling
$loadRoutes()
after the adhoc route declaration.
a
Aah interesting, @Peter Amiri... yes you nailed the requirement (also cc @David Belanger, as it answers your question to me too). Unfortunately we have needed to move on from this as it was just for a test, and we decided it required too much arsing about to be a good use of our time, so we have foregone this particular necessity. I'm intrigued to see whether it would work though, so might revisit it in my spare time & report back. Won't be this week though.
Although looking at the implementation (https://github.com/cfwheels/cfwheels/blob/v1.4.5/wheels/global/internal.cfm#L876):
Copy code
<cffunction name="$loadRoutes" returntype="void" access="public" output="false">
	<cfscript>
		var loc = {};
		loc.appKey = $appKey();

		// clear out the route info
		ArrayClear(application[loc.appKey].routes);
		StructClear(application[loc.appKey].namedRoutePositions);

		// load developer routes first
		$include(template="config/routes.cfm");

		// add the wheels default routes at the end if requested
		if (application[loc.appKey].loadDefaultRoutes)
		{
			addDefaultRoutes();
		}

		// set lookup info for the named routes
		$setNamedRoutePositions();
	</cfscript>
</cffunction>
To me it looks like it would take me further away from the solution. It blitzes everything in the
routes
array (which is gonna be the only place my newly added route would exist), and then just reloads the stuff from
config/routes.cfm
. It doesn't seem to do anything that looks like it "takes the route data from the
routes
array, and then [does something] to make them be actual routes that the app then "uses". Looking at how that
routes
array is used, it tracks back to https://github.com/cfwheels/cfwheels/blob/v1.4.5/wheels/dispatch/request.cfm#L86. and from superficial inspection, simply having the route in that array should be all that's needed. I will follow through that logic and see where things aren't going according to plan, and report back (well: maybe, depends if I too-hard-basket it).