FAQ: How do I get the base URL of the application?
$frontController = Zend_Controller_From::getInstance();
$baseUrl = $frontController->getBaseUrl();
FAQ: How do I generate URLs?
Use a view helper:
$view->url(array(
'controller' => 'index',
'action' => 'edit',
'id' => 777
));
$controller->_helper->getHelper('Redirector')->gotoRoute(array(
// ...
));
Note that the view object is available to the controller as $this->view.
FAQ: How do I generate content?
That is, what are my options for generating HTML from templates or otherwise? How can I generate partial content?
- $controller->render();
- $view->render();
- $view->partial();
- $view->action();
- A custom view helper
TIP: Useful helpers
- The ViewRenderer Action Helper (enabled by default).
FAQ: How to get custom view helpers to work?
Custom view helpers can be used to add extra methods to Zend Views. Once
added they're available in views as e.g. $this->fooBar().
The custom view helper is worth reading, but unfortunately a little light on the specifics.
The additional things you need to know are:
- By and large, helpers are loaded automatically--you don't need to "register" view helpers to make them available to views.
- By default, if you want your view helper to be invoked with the name
'fooBar()', then the method name needs to be
fooBar(), the classname needs to beZend_View_Helper_FooBarand the filename needs to beFooBar.php. The directory it needs to be put into is something like/webapp/modules/XXXX/views/helpersbut if ZF can't find your helper, it will report "Plugin by name 'XXXX' was not found in the registry" and then list the paths that it's trying. Check these paths to see where it's looking! - If you want to change the name of the class, or the directories that
are searched for view helpers,
$view->addHelperPath()or$view->setHelperPath().
ERROR: "No default module defined for this application"
You probably need to tell your front controller where to find your controllers. e.g.
$front->addControllerDirectory('../modules/foo/controllers');
FAQ: Zend_Rest_Route is behaving unexpectedly
Note that the action that's ultimately dispatched to may not
necessarily be the HTTP action used. The _method parameter is
consulted, as is the X-HTTP-Method-Override header. These two are
somewhat expected, but in certain circumstances, the URL is also used.
For example, if you attempt to POST to a "resource" rather than a
"collection" (e.g. POST /user/332), then the action will be
putAction rather than the expected postAction.
HOWTO: Testing exceptions in Zend Framework controller actions
Often you want to check that an action has thrown an exception, but
typically this is smothered by some error-handling code. (Because
there's no real "exception" in the HTTP response.) To accomplish this,
do the dispatch as usual, then check for the exception you're looking
for using the getException() method of the response.