Tag Archives: RTFM

CakePHP 2.x routing

One of the larger issues I’ve had to deal with after being rehired is that the commissioning system we created had stopped producing reports, you could load the page, but no data would populate. These reports are populated via ajax, so I checked the request and found the error “No data found”. Several hours of digging and google slowly uncovered what was happening behind the scenes.

The root cause ended up being the url of the ajax request to pull the data. There were colons in the url. They were already encoded, but once you removed the time section of the datetime string, the reports worked again. These reports have remained unchanged since they were first developed over a year ago. The issue lay in the core of CakePHP. In particular the way the routing for requests had changed. We had been using 2.1 before and are now using 2.7. The two routing classes simply decode the url in different places, and the new one does it before the elements are turned into arguments passed to the controller.

I ended up coming up with two fixes. The first was to simply rename the old CakeRoute and use it as a custom routing class. This worked, but I didn’t test it heavily so there may have been issues. In addition, I was unable to find a method to restrict the custom routing class to only the controllers that needed it. The second method was to simply write a router connection in Config/routes.php where I defined the argument elements. This required a call for each controller action I wanted to fix and would end up being time intensive if you needed to manage a large number of routes, I only had four. You can see my routing code below.

Software and versions used or mentioned:
CakePHP 2.1 & 2.7

CakePHP 2.x Routing Documentation
CakePHP GitHub