* @author Bertrand Chevrier */ class FlowController extends ClearFwFlowController implements ServiceManagerAwareInterface { use ServiceManagerAwareTrait; /** * This header is added to the response to inform the client a forward occurs */ const FORWARD_HEADER = 'X-Tao-Forward'; /** * Forward the action to execute reqarding a URL * The forward runs into tha same HTTP request unlike redirect. * @param string $url the url to forward to */ public function forwardUrl($url) { //get the current request $request = common_http_Request::currentRequest(); $params = $request->getParams(); //parse the given URL $parsedUrl = parse_url($url); //if new parameters are given, then merge them if (isset($parsedUrl['query']) && strlen($parsedUrl['query']) > 0) { $newParams = []; parse_str($parsedUrl['query'], $newParams); if (count($newParams) > 0) { $params = array_merge($params, $newParams); } } //resolve the given URL for routing $resolver = new Resolver(new common_http_Request($parsedUrl['path'], $request->getMethod(), $params)); $this->propagate($resolver); $context = Context::getInstance(); // load the responsible extension common_ext_ExtensionsManager::singleton()->getExtensionById($resolver->getExtensionId()); //update the context to the new route $context->setExtensionName($resolver->getExtensionId()); $context->setModuleName($resolver->getControllerShortName()); $context->setActionName($resolver->getMethodName()); if (count($params) > 0) { $context->getRequest()->addParameters($params); } //add a custom header so the client knows where the route ends header(self::FORWARD_HEADER . ': ' . $resolver->getExtensionId() . '/' . $resolver->getControllerShortName() . '/' . $resolver->getMethodName()); //execite the new action $enforcer = new ActionEnforcer($resolver->getExtensionId(), $resolver->getControllerClass(), $resolver->getMethodName(), $params); $this->propagate($enforcer); $enforcer->execute(); //should not be reached throw new InterruptedActionException( 'Interrupted action after a forward', $context->getModuleName(), $context->getActionName() ); } /** * Forward routing. * @param string $action the name of the new action * @param string $controller the name of the new controller/module * @param string $extension the name of the new extension * @param array $params additional parameters */ public function forward($action, $controller = null, $extension = null, $params = []) { //as we use a route resolver, it's easier to rebuild the URL to resolve it $this->forwardUrl(\tao_helpers_Uri::url($action, $controller, $extension, $params)); } }