当前位置: 首页>>代码示例>>PHP>>正文


PHP AbstractActionController::getMethodFromAction方法代码示例

本文整理汇总了PHP中Zend\Mvc\Controller\AbstractActionController::getMethodFromAction方法的典型用法代码示例。如果您正苦于以下问题:PHP AbstractActionController::getMethodFromAction方法的具体用法?PHP AbstractActionController::getMethodFromAction怎么用?PHP AbstractActionController::getMethodFromAction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Zend\Mvc\Controller\AbstractActionController的用法示例。


在下文中一共展示了AbstractActionController::getMethodFromAction方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: __invoke

 /**
  * Run an action from the specified controller and render it's associated template or view model
  * @param string $expr
  * @param array $attributes
  * @param array $options
  * @return string
  */
 public function __invoke($expr, $attributes, $options)
 {
     $serviceManager = $this->serviceLocator;
     $application = $serviceManager->get('Application');
     //parse the name of the controller, action and template directory that should be used
     $params = explode(':', $expr);
     $controllerName = $params[0];
     $actionName = 'not-found';
     if (isset($params[1])) {
         $actionName = $params[1];
     }
     //instantiate the controller based on the given name
     $controller = $serviceManager->get('ControllerLoader')->get($controllerName);
     //clone the MvcEvent and route and update them with the provided parameters
     $event = $application->getMvcEvent();
     $routeMatch = clone $event->getRouteMatch();
     $event = clone $event;
     $event->setTarget($controller);
     $routeMatch->setParam('action', $actionName);
     foreach ($attributes as $key => $value) {
         $routeMatch->setParam($key, $value);
     }
     $event->setRouteMatch($routeMatch);
     $actionName = $routeMatch->getParam('action');
     //inject the new event into the controller
     if ($controller instanceof InjectApplicationEventInterface) {
         $controller->setEvent($event);
     }
     //test if the action exists in the controller and change it to not-found if missing
     $method = AbstractActionController::getMethodFromAction($actionName);
     if (!method_exists($controller, $method)) {
         $method = 'notFoundAction';
         $actionName = 'not-found';
     }
     //call the method on the controller
     $response = $controller->{$method}();
     //if the result is an instance of the Response class return it
     if ($response instanceof Response) {
         return $response->getBody();
     }
     //if the response is an instance of ViewModel then render that one
     if ($response instanceof ModelInterface) {
         $viewModel = $response;
     } elseif ($response === null || is_array($response) || $response instanceof \ArrayAccess || $response instanceof \Traversable) {
         $viewModel = new ViewModel($response);
     } else {
         return '';
     }
     //inject the view model into the MVC event
     $event->setResult($viewModel);
     //inject template name based on the matched route
     $injectTemplateListener = new InjectTemplateListener();
     $injectTemplateListener->injectTemplate($event);
     $viewModel->terminate();
     $viewModel->setOption('has_parent', true);
     //render the view model
     $view = $serviceManager->get('Zend\\View\\View');
     $output = $view->render($viewModel);
     return $output;
 }
开发者ID:2eye-studios,项目名称:development,代码行数:67,代码来源:Render.php

示例2: renderAction

 /**
  * Render an action from a controller and render it's associated template
  * @param  string $expr
  * @param  array  $attributes
  * @param  array  $options
  * @return string
  */
 public function renderAction($expr, array $attributes, array $options)
 {
     ArgValidator::assert($expr, 'string');
     $serviceManager = Module::getServiceManager();
     $application = $serviceManager->get('Application');
     //parse the name of the controller, action and template directory that should be used
     if (strpos($expr, '/') > 0) {
         list($controllerName, $actionName) = explode('/', $expr);
         $templateDir = $controllerName . '/';
     } else {
         list($moduleName, $controllerName, $actionName) = explode(':', $expr);
         $actionName = lcfirst($actionName);
         $actionName = strtolower(preg_replace('/([A-Z])/', '-$1', $actionName));
         $templateDir = lcfirst($moduleName) . '/' . lcfirst($controllerName) . '/';
         $controllerName = $moduleName . '\\Controller\\' . $controllerName . 'Controller';
     }
     //instantiate the controller based on the given name
     $controller = $serviceManager->get('ControllerLoader')->get($controllerName);
     //clone the MvcEvent and route and update them with the provided parameters
     $event = $application->getMvcEvent();
     $routeMatch = clone $event->getRouteMatch();
     $event = clone $event;
     foreach ($attributes as $key => $value) {
         $routeMatch->setParam($key, $value);
     }
     $event->setRouteMatch($routeMatch);
     //inject the new event into the controller
     if ($controller instanceof InjectApplicationEventInterface) {
         $controller->setEvent($event);
     }
     //test if the action exists in the controller and change it to not-found if missing
     $method = AbstractActionController::getMethodFromAction($actionName);
     if (!method_exists($controller, $method)) {
         $method = 'notFoundAction';
         $actionName = 'not-found';
     }
     //call the method on the controller
     $response = $controller->{$method}();
     //if the result is an instance of the Response class return it
     if ($response instanceof Response) {
         return $response->getBody();
     }
     //if the response is an instance of ViewModel then render that one
     if ($response instanceof ModelInterface) {
         $viewModel = $response;
     } elseif ($response === null || is_array($response) || $response instanceof \ArrayAccess || $response instanceof \Traversable) {
         $viewModel = new ViewModel($response);
         $viewModel->setTemplate($templateDir . $actionName);
     } else {
         return '';
     }
     $viewModel->terminate();
     $viewModel->setOption('has_parent', true);
     $view = $serviceManager->get('Zend\\View\\View');
     $output = $view->render($viewModel);
     return $output;
 }
开发者ID:heartsentwined,项目名称:zf2-assetwig,代码行数:64,代码来源:Extension.php

示例3: getMethodFromAction

 /**
  * {@inheritDoc}
  * @see \Zend\Mvc\Controller::getMethodFromAction($action)
  */
 public static function getMethodFromAction($action) : string
 {
     $methodName = parent::getMethodFromAction($action);
     if (method_exists(get_called_class(), $methodName)) {
         $methodName = 'invokeAction';
     }
     return $methodName;
 }
开发者ID:geolysis,项目名称:gz3-base,代码行数:12,代码来源:AbstractActionController.php


注:本文中的Zend\Mvc\Controller\AbstractActionController::getMethodFromAction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。