Question

from a little knowledge of chwheels i have, i know that routes are stored in the application scope i.e. in application.wheels.routes to be precise. It also maintains the route position in application.wheels.namedroutepositions.

Now i have added route i.e. controller,action,pattern,name in application.wheels.routes and also added in the namedRoutePositions structure. as follows:

<cfset myRoute.controller = 'pages' >
<cfset myRoute.action = 'about' >
<cfset myRoute.pattern = 'about_my_project' >
<cfset myRoute.name = 'about' >

<cfset structAppend(application.wheels.routes[3],myRoute) />
<cfset structInsert(application.wheels.namedroutepositions,'about','3',true)/>

the above code is added to settings.cfc which i invoked on the application start. If i dumped the application.wheels.routes and application.wheels.namedroutepositions in settings.cfc after adding the above code, it is reflected in those respective structures but when i tried to access the the new route i just added, it doesn't work. Also, when i dumped the application.wheels.routes and application.wheels.namedroutepositions in another cfc, it didn't showed the newly added route i.e. the newly added route didn't persist. how to make newly added route to persist throughout the application? I am not sure what went wrong. Is there any other variable/structure where i need to update route information? Is there anything else i need to do to make this work?

Note: for time being, i have added this route manually. When this experiment become successfull, i will make this code to add route dynamically.

Thanks for all help in advance.

Was it helpful?

Solution

In design and development modes, I believe that the routes are re-generated on every request, which is why you're losing the value in the application scope.

That said, the application scope would not be a good place to persist your routes because the application scope is refreshed when your application is reloaded. (You probably already knew that though.)

One way that you can create a fully dynamic route is like this (though I admit it is a little ugly):

<cfset addRoute(name="page", pattern="[folder1]/[folder2]/[folder3]/[folder4]/[folder5]", controller="pages", action="show")>
<cfset addRoute(name="page", pattern="[folder1]/[folder2]/[folder3]/[folder4]", controller="pages", action="show")>
<cfset addRoute(name="page", pattern="[folder1]/[folder2]/[folder3]", controller="pages", action="show")>
<cfset addRoute(name="page", pattern="[folder1]/[folder2]", controller="pages", action="show")>
<cfset addRoute(name="page", pattern="[folder1]", controller="pages", action="show")>
<cfset addRoute(name="root", pattern="", controller="someController", action="someAction")>

Then if the routing needs to be controlled dynamically via database, you can do the appropriate querying in controllers/Pages.show() with params.folder1 through params.folder5.

Notice that these dynamic routes should go after any other route that you need to specify, but just before the empty root route.

You'll also want to look and see if the ColdRoute plugin that Tom mentions in his answer does anything with wildcard routes.

Something like this is most appropriate because the routes should be hard-coded in config/routes.cfm. Do not fight this.

OTHER TIPS

At the risk of stating the obvious, why are you going against wheels convention here?

Why not add stuff in config/routes.cfm using the conventions as outlined in http://cfwheels.org/docs/1-1/chapter/using-routes ?

You can do all sorts of 'dynamic' routes, i.e

addRoute(name="product", pattern="products/[categorySlug]/[productSlug]", controller="products", action="product");

I may have misunderstood what you're trying to achieve though. If you really want to muck around with the internal wheels stuff, have a look at the coldroute plugin: http://cfwheels.org/plugins/listing/67

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top