Search code examples
zend-frameworkzend-viewcontext-switch

How does zend view render resolve path to view script


How does an action like this "customSearchAction()" map to the view script file name.

Neither of these file names work "customsearch.xml.phtml", "customSearch.xml.phtml", "custom-search.xml.phtml".

Please, note that I am using context switching view helper for xml, json. Also, the module and action are resolving properly. When I change the action name to "customsearchAction()" and name the rename the script file to "customsearch.xml.phtml", then it works.

So how is the view script file name resolved in the above case? in the Zend Framework


Solution

  • From documentation:

    Note: Naming Conventions: Word Delimiters in Controller and Action Names If your controller or action name is composed of several words, the dispatcher requires that these are separated on the URL by specific path and word delimiter characters. The ViewRenderer replaces any path delimiter found in the controller name with an actual path delimiter ('/'), and any word delimiter found with a dash ('-') when creating paths. Thus, a call to the action /foo.bar/baz.bat would dispatch to FooBarController::bazBatAction() in FooBarController.php, which would render foo-bar/baz-bat.phtml; a call to the action /bar_baz/baz-bat would dispatch to Bar_BazController::bazBatAction() in Bar/BazController.php (note the path separation) and render bar/baz/baz-bat.phtml. Note that the in the second example, the module is still the default module, but that, because of the existence of a path separator, the controller receives the name Bar_BazController, in Bar/BazController.php. The ViewRenderer mimics the controller directory hierarchy.

    So, from that custom-search.phtml is the right name. Now you are maybe wrong with the directory where you store it, what's the controller name (and module)? Check as well you view script is readable by apache.

    Edit

    In the case of ContextSwitch usage:

    • the initialisation is: $contextSwitch->addActionContext('custom-search', 'xml');
    • the view script is custom-search.xml.phtml

    Just tested it on a ZF 1.6 (old but should still be valid). So maybe your initialization is wrong (used 'customSearch' instead of 'custom-search'?).