ZF-3053: The plugin solution to the exceptions handling doesn't work
Description
<?php
class My_Controller_PreDispatchPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
$controller = $dispatcher->getController($request);
if (!$controller) {
$controller = $dispatcher->getDefaultControllerName($request);
}
$action = $dispatcher->getAction($request);
if (!method_exists($controller, $action)) {
$defaultAction = $dispatcher->getDefaultAction();
$controllerName = $request->getControllerName();
$response = Zend_Controller_Front::getInstance()->getResponse();
$response->setRedirect('/' . $controllerName . '/' . $defaultAction);
$response->sendHeaders();
exit;
}
}
}
The dispatcher doesn't have a function getController() it does however have a getControllerClass which will return the className or false which is nice but still won't give you an initialized class to use for the action checking.
Also the headering to /:controller/:action is nice but it should be reverse routed first and have the $request->getBaseUrl() prepended.
I know it's example code but a proper working example would be nice.
Comments
Posted by Wil Sinclair (wil) on 2008-04-18T17:05:56.000+0000
Please evaluate and categorize/assign as necessary.
Posted by Ralph Schindler (ralph) on 2008-04-22T11:56:36.000+0000
Harro,
I am not too clear on what it is your plugin is trying to accomplish.. If you could explain your use case in a bit more detail, it would be easier to understand where you feel the bug is. Have you checked Matthews tutorial on the subject:
http://devzone.zend.com/article/…
perhaps that might help.
Thanks, Ralph
Posted by Ralph Schindler (ralph) on 2008-04-22T11:57:10.000+0000
Unassigning until more info comes in
Posted by Harro van der Klauw (oximoron) on 2008-04-22T12:48:52.000+0000
The idea is that it will check to see if the controller and action specified actually exist on the preDispatch. That way you can handle non existing actions and/or controllers in one place instead of getting and parsing the specific exception in the error handler.
It's not that I really need it, it's more that it's posted as example in the docs and it doesn't even work.
It's in paragraph 7.12.3 in the current docs.
Posted by Wil Sinclair (wil) on 2008-06-09T13:13:46.000+0000
Updating component to conform to new IT conventions.
Posted by Wil Sinclair (wil) on 2008-06-09T13:27:46.000+0000
I'm not entirely sure if this is an existing example or a proposed new example. Could the reporter clarify?
Posted by Harro van der Klauw (oximoron) on 2008-06-09T23:20:47.000+0000
it's an existing non working example in paragraph 7.12.3
http://framework.zend.com/manual/en/… (at the bottom)
It should be changed to working code, or it should be removed because it does not work.
Posted by Matthew Weier O'Phinney (matthew) on 2008-11-25T04:20:04.000+0000
Fixed in trunk with r12821; will likely update on site with 1.7.1 release.