Skip to content

Instantly share code, notes, and snippets.

@cotcotquedec
Created October 3, 2014 08:14
Show Gist options
  • Save cotcotquedec/cb235d6b3198dddaafff to your computer and use it in GitHub Desktop.
Save cotcotquedec/cb235d6b3198dddaafff to your computer and use it in GitHub Desktop.
Zend Framework 1 - Inject params into action methods
<?php
/**
* Class YourAbstractControllerClass
*
* Overload Zend_Controller_Action to inject params into action methods
*
*/
abstract class YourAbstractControllerClass extends Zend_Controller_Action
{
/**
* Dispatch the requested action
*
* @param string $action Method name of action
* @return void
*/
public function dispatch($action)
{
// Notify helpers of action preDispatch state
$this->_helper->notifyPreDispatch();
$this->preDispatch();
if ($this->getRequest()->isDispatched()) {
if (null === $this->_classMethods) {
$this->_classMethods = get_class_methods($this);
}
// If pre-dispatch hooks introduced a redirect then stop dispatch
// @see ZF-7496
if (!($this->getResponse()->isRedirect())) {
// preDispatch() didn't change the action, so we can continue
if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
if ($this->getInvokeArg('useCaseSensitiveActions')) {
trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
}
/// MODIFICATION - START //
// Reflection on action method
$method = new ReflectionMethod($this, $action);
$params = [];
foreach ($method->getParameters() as $p) {
/** @var ReflectionParameter $p */
$params[] = $this->hasParam($name = $p->getName()) ? $this->getParam($name) : $p->getDefaultValue();
}
// calling the method
call_user_func_array([$this, $action], $params);
/// MODIFICATION - END //
} else {
$this->__call($action, array());
}
}
$this->postDispatch();
}
// whats actually important here is that this action controller is
// shutting down, regardless of dispatching; notify the helpers of this
// state
$this->_helper->notifyPostDispatch();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment