>
> Does isDispatchable not check if the action exists to support actions via
> the controllers __call method?
>
Hi,
I just had the same problem. I have 2 modules: One for everyone, and one
only for authenticated users.
Now I try to find out if a (not authenticated) user visits a url (let's say
/document/view/), whether the corresponding controller and action exist in
the module for authenticated users..
Based on this information I can:
- Decide whether to display a 404-message or a "you need to login"-message.
- Decide whether to forward the user directly to the url again, after
he/she logged in.
I've now written the following method in a Controller-Plugin to determine,
whether a Request will be a valid module/controller/action-combination. I'm
calling it in preDispatch().
Maybe it helps someone, maybe someone has comments .. :)
Sincerely,
reto
/**
* Return whether a given request (module-controller-action) exists
*
* @param Zend_Controller_Request_Abstract $request Request to check
* @return boolean Whether the action exists
*/
private function _actionExists($request) {
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
// Check for default controller
if ($request->getControllerName() ==
$dispatcher->getDefaultControllerName()) {
return true;
}
// Check controller
if (!$dispatcher->isDispatchable($request)) {
return false;
}
// Check action
$controllerClassName = $dispatcher->formatControllerName(
$request->getControllerName() );
$controllerClassFile = $controllerClassName . '.php';
if ($request->getModuleName() != $dispatcher->getDefaultModule()) {
$controllerClassName = ucfirst($request->getModuleName()) . '_' .
$controllerClassName;
}
try {
require_once 'Zend/Loader.php';
Zend_Loader::loadFile($controllerClassFile,
$dispatcher->getControllerDirectory());
$actionMethodName =
$dispatcher->formatActionName($request->getActionName());
if (@in_array($actionMethodName,
get_class_methods($controllerClassName))) {
return true;
}
return false;
} catch(Exception $e) {
return false;
}
}
--
View this message in context: http://www.nabble.com/should-isDispatchable-check-if-the-action-exists--tp15648297p19634286.html
Sent from the Zend MVC mailing list archive at Nabble.com.
没有评论:
发表评论