开发者

Case insensitive url-routing for Zend

I am working with legacy code written in PHP 5.2.6 with Zend Framework 1.5.3 on CentOS 5.5

There is code as such

url:"./sales/getAlerts/?bypass=1"

The view is named getalerts.phtml

Controller function is named getAlertsAction

Code works fine on existing server, but when I try moving it to a new server the code fails stating that ...'Action "getalerts" does not exist...

If I change the lines mentioned above to make the [A] in Alerts lower case the code works.

I've copied over php.in开发者_如何学Goi, httpd.conf, and .htaccess. Not sure what else could allow case insensitive mapping to occur.

What is it that allows url-routing to be case insensitive?


Your URL: /sales/getAlerts/?bypass=1

Actually expands into:

  • Controller: SalesController
  • Action: getalertsAction
  • View: sales/getalerts.phtml

The issue is you're naming your action method slightly wrong, so if the old/local server isn't case-sensitive (most PCs and Macs are case-insensitive) when tested locally it doesn't matter. When published to a webserver (likely UNIX) it's case-sensitive so it breaks.

What you actually want to do is keep all URLs lower-case and separate words with dashes.

If you use the URL: /sales/get-alerts/?bypass=1

It expands into:

  • Controller: SalesController
  • Action: getAlertsAction
  • View: sales/get-alerts.phtml

While you can change this behaviour you're better off sticking to the ZF defaults with your naming conventions.


You should be able to use any URLs you want in your routes... I'm guessing you are using the default /:controller/:action/* route? If so, create custom ones to point to the lower-case actions that you've defined.

The confusion lies in mapping your route parameters to your actual controllers/actions/modules. Each 'word' is broken down into camelCase for the controller action, but everywhere else you have to use the hyphen to separate words.

test = testAction = test.phtml test-something = testSomethingAction = test-something.phtml

You should be able to modify this behaviour. Check out Zend_Controller_Dispatch_Abstract::formatActionName(). However, ideally, you'd just want to modify your route.


Usually in Zend, it's a good habit to name your action's function in lower case (apart from the 'A' of Action). (At least, that's what I do to avoid error)

The url can be with "getAlerts", Zend will search for the "getalertsAction" function in the controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜