2
votes

I'm using CakePHP 2.1.0 and I noticed that if you want to access a controller's action then it doesn't matter what casing you use for the action name; you will always hit that action in the controller. However, the action will expect the view file to be named with the same casing that you used to access the action. So if I were to go to http://example.com/users/aDd, I would hit the code in the "Users" controller's "add" action as expected, but it would look for aDd.ctp, which doesn't exist. Is there a way to make it so that action names can only be accessed if they're lowercase or else they're considered a bad URL?

UPDATE: I think it's safest to do this on the CakePHP level rather than on the web server level. This way, if, for whatever reason, you want to have http://example.com/FoO be valid regardless of the casing but you want http://example.com/bar and http://example.com/users/add to only be accessible if lowercase, you can do that.

Does anybody see a flaw in adding this to the "App" controller's "beforeFilter" method?:

if ($this->request->params['controller'] != strtolower($this->request->params['controller']) || $this->action != strtolower($this->action)) {
    $redirect = Router::url(array(
        'controller' => strtolower($this->request->params['controller']),
        'action' => strtolower($this->action)
    ) + $this->request->params['pass']);

    foreach ($this->request->params['named'] as $key => $value) {
        $redirect .= "/$key:$value";
    }

    $this->redirect($redirect);
}
1
In this scenario, I think it would be friendlier to just add a few lines of code that get the Controller to load the lowercased version (add.ctp); a bonus would be to rewrite the URL in the browser just to make it prettier.starlocke
Just a guess, you can probably catch them somehow with some matching in router defintions, see book.cakephp.org/2.0/en/development/routing.html . Or if this doesn't work, there's always mod_rewrite or similar, depending on your webserver.C. Ramseyer
If you always build your links in the views in lowercase, who would visit your website with urls cased differently ? I'm not sure that it worths it to run this code on every request to be useful what, maybe once or two ?nIcO
@nIcO I totally get what you're saying, but keep in mind that, although most people copy and paste URLs, some people handle them differently. For example, a person might read it over the phone to somebody else and that person might have caps lock on, for example. Then that person could like what they see and then post that link on Facebook for a bunch of other people to click on. Google considers URLs with different casing to be unique, so that could screw up your ranking, from what I understand. Yeah, not very likely, but still something to consider.Nick

1 Answers

1
votes

Well. How about these lines in your AppController:

public function beforeFilter() {
  $this->view = strtolower($this->view);
}