C Case sensitivity

Zend Framework's router and dispatcher are sensitive to the case of action and controller names. This is for a very good reason: PHP's function and method names aren't case sensitive, but filenames generally are. This means that there are rules when using capital letters or other word separators as controller names to ensure that the correct class files can be found. For action names, it's important that the correct view script can be found. The following subsections outline the rules for controller and action URLs. Word separation within controller URLS

The first letter of a controller class name must be uppercase—this is enforced by the dispatcher. The dispatcher also converts certain word-separator characters into directory separators when determining the filename for the class. Table C.1 shows the effect of different controller URLs on the filename and class name of the controller for a controller we wish to call "tech support".

Table C.1 Mapping of controller URLs to controller class name and filename

Action URL

Controller class name

Controller filename

/techsupport/

TechsupportController

TechsupportController.php

/techSupport/

TechsupportController

TechsupportController.php

/tech-support/

TechSupportController

TechSupportController.php

/tech.support/

TechsupportController

TechSupportController.php

Table C.1 Mapping of controller URLs to controller class name and filename (continued)

Action URL

Controller class name

Controller filename

/tech_support/

Tech SupportController

Tech/SupportController.php

As you can see in table C.1, capital letters in controller names are always lowercased, and only period (.) and hyphen (-) word separators will result in a MixedCased class name and filename. In all cases, there is a direct mapping of class name to filename. This follows the standard Zend Framework conventions, so if an underscore is used in the controller's URL, it acts as a directory separator on disk.

Action names have similar but slightly different rules, because both the dispatcher and the ViewRenderer are involved in resolving an action URL through to an action method and then on to the associated view script file. Word separation within action URLS

The dispatcher enforces case sensitivity in action method names. This means that it will expect your action method name to be lowercase unless you separate the words in the URL with a recognized word separator. By default, only the period (.) and the hyphen (-) are recognized word separators.

This means that if you name your action using camelCase naming (such as viewDetails), the action called will be the all lowercase viewdetailsAction(). The View-Renderer will map both recognized word separators and also a capital letter change to a hyphen within the view script filename. This means that an action URL of viewDetails will be mapped to a view script filename of view-details.phtml.

Table C.2 shows the effect of different URL word separators on the action and view script called.

Table C.2 Mapping of action URLs to action functions and view script filenames

Action URL

Action function

View script filename

/viewdetails

viewdetailsAction()

viewdetails.phtml

/viewDetails

viewdetailsAction()

view-details.phtml

/view_details

viewdetailsAction()

view-details.phtml

/view-details

viewDetailsAction()

view-details.phtml

/view.details

viewDetailsAction()

view-details.phtml

As you can see from table C.3, a certain amount of care needs to be taken when using word separators in URLs. We recommend that you always use a hyphen in your URL if you want to have word separation. This will result in easy-to-read URLs, camelCase action function names, and a hyphen separator in your view script filenames, which is consistent with the action name in the URL.

The rules for word separation in controller and action names follow a predictable system. In general, using the hyphen is easiest and also results in URL separations that Google and other search engines like, so we recommend you use them if you want to separate out multiword controller and action names.

Let's look at how to use the router to our advantage for both speed and flexibility in converting URLs to action functions.

0 0

Post a comment

  • Receive news updates via email from this site