/** * Before dispatching, digest PUT request body and set params * * @param \Zend\Controller\Request\AbstractRequest $request */ public function preDispatch(\Zend\Controller\Request\AbstractRequest $request) { if (!$request instanceof \Zend\Controller\Request\Http) { return; } if ($this->_request->isPut()) { $putParams = array(); parse_str($this->_request->getRawBody(), $putParams); $request->setParams($putParams); } }
/** * Retrieve rendered contents of a controller action * * If the action results in a forward or redirect, returns empty string. * * @param string $action * @param string $controller * @param string $module Defaults to default module * @param array $params * @return string */ public function __invoke($action, $controller, $module = null, array $params = array()) { $this->resetObjects(); if (null === $module) { $module = $this->defaultModule; } // clone the view object to prevent over-writing of view variables $broker = $this->front->getHelperBroker(); $viewRenderer = $broker->load('viewRenderer'); $viewRendererClone = clone $viewRenderer; $broker->register('viewRenderer', $viewRendererClone); $this->request->setParams($params) ->setModuleName($module) ->setControllerName($controller) ->setActionName($action) ->setDispatched(true); $this->dispatcher->dispatch($this->request, $this->response); // reset the viewRenderer object to it's original state $broker->register('viewRenderer', $viewRenderer); if (!$this->request->isDispatched() || $this->response->isRedirect()) { // forwards and redirects render nothing return ''; } $return = $this->response->getBody(); $this->resetObjects(); return $return; }
/** * Reset the request object * * Useful for test cases that need to test multiple trips to the server. * * @return \Zend\Test\PHPUnit\ControllerTestCase */ public function resetRequest() { if ($this->_request instanceof Request\HttpTestCase) { $this->_request->clearQuery()->clearPost(); } $this->_request = null; return $this; }
/** * Set request keys based on values in request object * * @return void */ protected function _setRequestKeys() { if (null !== $this->_request) { $this->_moduleKey = $this->_request->getModuleKey(); $this->_controllerKey = $this->_request->getControllerKey(); $this->_actionKey = $this->_request->getActionKey(); } if (null !== $this->_dispatcher) { $this->_defaults += array($this->_controllerKey => $this->_dispatcher->getDefaultControllerName(), $this->_actionKey => $this->_dispatcher->getDefaultAction(), $this->_moduleKey => $this->_dispatcher->getDefaultModule()); } $this->_keysSet = true; }
/** * Retrieve rendered contents of a controller action * * If the action results in a forward or redirect, returns empty string. * * @param string $action * @param string $controller * @param string $module Defaults to default module * @param array $params * @return string */ public function direct($action = null, $controller = null, $module = null, array $params = array()) { if ($action == null || $controller == null) { throw new \InvalidArgumentException('Action: missing argument. $action and $controller are required in action($action, $controller, $module = null, array $params = array())'); } $this->resetObjects(); if (null === $module) { $module = $this->defaultModule; } // clone the view object to prevent over-writing of view variables $viewRendererObj = HelperBroker\HelperBroker::getStaticHelper('viewRenderer'); HelperBroker\HelperBroker::addHelper(clone $viewRendererObj); $this->request->setParams($params)->setModuleName($module)->setControllerName($controller)->setActionName($action)->setDispatched(true); $this->dispatcher->dispatch($this->request, $this->response); // reset the viewRenderer object to it's original state HelperBroker\HelperBroker::addHelper($viewRendererObj); if (!$this->request->isDispatched() || $this->response->isRedirect()) { // forwards and redirects render nothing return ''; } $return = $this->response->getBody(); $this->resetObjects(); return $return; }
/** * Handle errors and exceptions * * If the 'noErrorHandler' front controller flag has been set, * returns early. * * @param \Zend\Controller\Request\AbstractRequest $request * @return void */ protected function _handleError(Request\AbstractRequest $request) { $frontController = Controller\Front::getInstance(); if ($frontController->getParam('noErrorHandler')) { return; } $response = $this->getResponse(); if ($this->_isInsideErrorHandlerLoop) { $exceptions = $response->getException(); if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) { // Exception thrown by error handler; tell the front controller to throw it $frontController->throwExceptions(true); throw array_pop($exceptions); } } // check for an exception AND allow the error handler controller the option to forward if ($response->isException() && !$this->_isInsideErrorHandlerLoop) { $this->_isInsideErrorHandlerLoop = true; // Get exception information $error = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS); $exceptions = $response->getException(); $exception = $exceptions[0]; $exceptionType = get_class($exception); $error->exception = $exception; switch ($exceptionType) { case 'Zend\\Controller\\Router\\Exception': if (404 == $exception->getCode()) { $error->type = self::EXCEPTION_NO_ROUTE; } else { $error->type = self::EXCEPTION_OTHER; } break; case 'Zend\\Controller\\Dispatcher\\Exception': $error->type = self::EXCEPTION_NO_CONTROLLER; break; case 'Zend\\Controller\\Action\\Exception': if (404 == $exception->getCode()) { $error->type = self::EXCEPTION_NO_ACTION; } else { $error->type = self::EXCEPTION_OTHER; } break; default: $error->type = self::EXCEPTION_OTHER; break; } // Keep a copy of the original request $error->request = clone $request; // get a count of the number of exceptions encountered $this->_exceptionCountAtFirstEncounter = count($exceptions); // Forward to the error handler $request->setParam('error_handler', $error)->setModuleName($this->getErrorHandlerModule())->setControllerName($this->getErrorHandlerController())->setActionName($this->getErrorHandlerAction())->setDispatched(false); } }
/** * Dispatch an HTTP request to a controller/action. * * @param \Zend\Controller\Request\AbstractRequest|null $request * @param \Zend\Controller\Response\AbstractResponse|null $response * @return void|\Zend\Controller\Response\AbstractResponse Returns response object if returnResponse() is true */ public function dispatch(Request\AbstractRequest $request = null, Response\AbstractResponse $response = null) { $helperBroker = $this->getHelperBroker(); if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('\Zend\Controller\Plugin\ErrorHandler')) { // Register with stack index of 100 $this->_plugins->registerPlugin(new Plugin\ErrorHandler(), 100); } if (!$this->getParam('noViewRenderer') && !$helperBroker->hasPlugin('viewRenderer')) { $viewRenderer = $helperBroker->load('viewrenderer'); $helperBroker->getStack()->offsetSet(-80, $viewRenderer); } /** * Instantiate default request object (HTTP version) if none provided */ if (null !== $request) { $this->setRequest($request); } elseif ((null === $request) && (null === ($request = $this->getRequest()))) { $request = new Request\Http(); $this->setRequest($request); } /** * Set base URL of request object, if available */ if (is_callable(array($this->_request, 'setBaseUrl'))) { if (null !== $this->_baseUrl) { $this->_request->setBaseUrl($this->_baseUrl); } } /** * Instantiate default response object (HTTP version) if none provided */ if (null !== $response) { $this->setResponse($response); } elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) { $response = new Response\Http(); $this->setResponse($response); } /** * Register request and response objects with plugin broker */ $this->_plugins ->setRequest($this->_request) ->setResponse($this->_response) ->setHelperBroker($helperBroker); /** * Initialize router */ $router = $this->getRouter(); $router->setParams($this->getParams()); /** * Initialize dispatcher */ $dispatcher = $this->getDispatcher(); $dispatcher->setParams($this->getParams()) ->setResponse($this->_response) ->setHelperBroker($helperBroker); // Begin dispatch try { /** * Route request to controller/action, if a router is provided */ /** * Notify plugins of router startup */ $this->_plugins->routeStartup($this->_request); try { $router->route($this->_request); } catch (\Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of router completion */ $this->_plugins->routeShutdown($this->_request); /** * Notify plugins of dispatch loop startup */ $this->_plugins->dispatchLoopStartup($this->_request); /** * Attempt to dispatch the controller/action. If the $this->_request * indicates that it needs to be dispatched, move to the next * action in the request. */ do { $this->_request->setDispatched(true); /** * Notify plugins of dispatch startup */ $this->_plugins->preDispatch($this->_request); /** * Skip requested action if preDispatch() has reset it */ if (!$this->_request->isDispatched()) { continue; } /** * Dispatch request */ try { $dispatcher->dispatch($this->_request, $this->_response); } catch (\Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of dispatch completion */ $this->_plugins->postDispatch($this->_request); } while (!$this->_request->isDispatched()); } catch (\Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of dispatch loop completion */ try { $this->_plugins->dispatchLoopShutdown(); } catch (\Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } if ($this->returnResponse()) { return $this->_response; } $this->_response->sendResponse(); }
/** * Determine the action name * * First attempt to retrieve from request; then from request params * using action key; default to default action * * Returns formatted action name * * @param \Zend\Controller\Request\AbstractRequest $request * @return string */ public function getActionMethod(Request\AbstractRequest $request) { $action = $request->getActionName(); if (empty($action)) { $action = $this->getDefaultAction(); $request->setActionName($action); } return $this->formatActionName($action); }
/** * Find a matching route to the current PATH_INFO and inject * returning values to the Request object. * * @throws \Zend\Controller\Router\Exception * @return \Zend\Controller\Request\AbstractRequest Request object */ public function route(\Zend\Controller\Request\AbstractRequest $request) { if (!$request instanceof \Zend\Controller\Request\HTTP) { require_once 'Zend/Controller/Router/Exception.php'; throw new Exception('Zend_Controller_Router_Rewrite requires a Zend_Controller_Request_HTTP-based request object'); } if ($this->_useDefaultRoutes) { $this->addDefaultRoutes(); } // Find the matching route $routeMatched = false; foreach (array_reverse($this->_routes) as $name => $route) { // TODO: Should be an interface method. Hack for 1.0 BC if (method_exists($route, 'isAbstract') && $route->isAbstract()) { continue; } // TODO: Should be an interface method. Hack for 1.0 BC if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) { $match = $request->getPathInfo(); } else { $match = $request; } if ($params = $route->match($match)) { $this->_setRequestParams($request, $params); $this->_currentRoute = $name; $routeMatched = true; break; } } if (!$routeMatched) { require_once 'Zend/Controller/Router/Exception.php'; throw new Exception('No route matched the request', 404); } if ($this->_useCurrentParamsAsGlobal) { $params = $request->getParams(); foreach ($params as $param => $value) { $this->setGlobalParam($param, $value); } } return $request; }
/** * Forward request with next action * * @param array $next * @return void */ public function forward(Request\AbstractRequest $next) { $request = $this->getRequest(); if ($this->getClearRequestParams()) { $request->clearParams(); } $request->setModuleName($next->getModuleName())->setControllerName($next->getControllerName())->setActionName($next->getActionName())->setParams($next->getParams())->setDispatched(false); }
public function createExternalFromRequest(\Zend\Controller\Request\AbstractRequest $request, $name, $hash = null) { $values = array(); $actionRequest = clone $request; $actionRequest->clearParams(); $sysmapId = $this->getExistingHashByRequest($actionRequest); if (!$sysmapId instanceof \Zend\Acl\Resource) { return false; } if (!empty($hash)) { $values['hash'] = $hash; } $values['sysmap_id'] = $sysmapId->getResourceId(); $values['name'] = $name; $values['params'] = $request->getParams(); return $this->saveExtension($values); }
/** * postDispatch() plugin hook -- render layout * * @param \Zend\Controller\Request\AbstractRequest $request * @return void */ public function postDispatch(\Zend\Controller\Request\AbstractRequest $request) { $layout = $this->getLayout(); $helper = $this->getLayoutActionHelper(); // Return early if forward detected if (!$request->isDispatched() || $this->getResponse()->isRedirect() || $layout->getMvcSuccessfulActionOnly() && (!empty($helper) && !$helper->isActionControllerSuccessful())) { return; } // Return early if layout has been disabled if (!$layout->isEnabled()) { return; } $response = $this->getResponse(); $content = $response->getBody(true); $contentKey = $layout->getContentKey(); if (isset($content['default'])) { $content[$contentKey] = $content['default']; } if ('default' != $contentKey) { unset($content['default']); } $layout->assign($content); $fullContent = null; $obStartLevel = ob_get_level(); try { $fullContent = $layout->render(); $response->setBody($fullContent); } catch (\Exception $e) { while (ob_get_level() > $obStartLevel) { $fullContent .= ob_get_clean(); } $request->setParam('layoutFullContent', $fullContent); $request->setParam('layoutContent', $layout->content); $response->setBody(null); throw $e; } }
/** * Set a userland parameter * * Uses $key to set a userland parameter. If $key is an alias, the actual * key will be retrieved and used to set the parameter. * * @param mixed $key * @param mixed $value * @return \Zend\Controller\Request\Http */ public function setParam($key, $value) { $key = null !== ($alias = $this->getAlias($key)) ? $alias : $key; parent::setParam($key, $value); return $this; }
/** * Push actions into application actions stack * @param int $id * @param Templater_Api_Interface_Widget $widget * @param string $placeholder * @param array $params */ protected function _pushStack($id, \Zend\Controller\Request\AbstractRequest $request, $placeholder, $params = array()) { $params[$this->_widgetIdName] = md5($id); $camelFilter = new \Zend\Filter\Word\CamelCaseToDash('-'); $blockRequest = new \Zend\Controller\Request\Simple(strtolower($camelFilter->filter($request->getActionName())), strtolower($camelFilter->filter($request->getControllerName())), strtolower($camelFilter->filter($request->getModuleName())), array_merge($params, array($this->_marker => $placeholder))); $this->_stack->pushStack($blockRequest); }
/** * Defined by Zend_Controller_Plugin_Abstract * * @param Zend_Controller_Request_Abstract * @return void */ public function postDispatch(AbstractRequest $request) { $this->mark($request->getControllerName() . 'Controller::' . $request->getActionName() . 'Action'); }