本文整理汇总了PHP中Zend\Mvc\MvcEvent::getControllerClass方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getControllerClass方法的具体用法?PHP MvcEvent::getControllerClass怎么用?PHP MvcEvent::getControllerClass使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getControllerClass方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onError
public function onError(MvcEvent $event)
{
// if error has been removed
if ($event->isError() == false) {
return;
}
$locator = $event->getApplication()->getServiceManager();
/* @var $errorService \BitWeb\ErrorReporting\Service\ErrorService */
$errorService = $locator->get('BitWeb\\ErrorReporting\\Service\\ErrorService');
$exception = $event->getParam('exception');
$error = $event->getError();
$stopPropagation = false;
if ($exception instanceof \Exception) {
$errorService->errors[] = $exception;
if ($exception instanceof \PDOException) {
$stopPropagation = true;
}
} elseif ($error != null) {
$errorService->errors[] = new RouterNoMatchException(sprintf('%1$s, %2$s', $error, $event->getControllerClass()));
}
if ($stopPropagation) {
$this->onFinishAfterPostDispatch($event);
}
}
示例2: injectController
/**
* Inject the controller and controller class into the model
*
* If either $displayExceptions or $displayNotFoundReason are enabled,
* injects the controllerClass from the MvcEvent. It checks to see if a
* controller is present in the MvcEvent, and, if not, grabs it from
* the route match if present; if a controller is found, it injects it into
* the model.
*
* @param ViewModel $model
* @param MvcEvent $e
* @return void
*/
protected function injectController($model, $e)
{
if (!$this->displayExceptions() && !$this->displayNotFoundReason()) {
return;
}
$controller = $e->getController();
if (empty($controller)) {
$routeMatch = $e->getRouteMatch();
if (empty($routeMatch)) {
return;
}
$controller = $routeMatch->getParam('controller', false);
if (!$controller) {
return;
}
}
$controllerClass = $e->getControllerClass();
$model->setVariable('controller', $controller);
$model->setVariable('controller_class', $controllerClass);
}
示例3: detectError
public function detectError(MvcEvent $event)
{
$result = $event->getResult();
if ($result instanceof ResponseInterface) {
// Already have a response as the result
return;
}
$error = $event->getError();
$ex = $event->getParam('exception');
switch ($error) {
case Application::ERROR_CONTROLLER_NOT_FOUND:
$message = ucfirst($event->getControllerClass());
$ex = new RdnException\RuntimeException($message, 500, $ex);
break;
case Application::ERROR_CONTROLLER_INVALID:
$message = $event->getController() . ' is not dispatchable';
$ex = new RdnException\RuntimeException($message, 500, $ex);
break;
case Application::ERROR_ROUTER_NO_MATCH:
$message = 'Request did not match any routes.';
$ex = new RdnException\NotFoundException($message, 404, $ex);
break;
}
if (isset($message)) {
if ($ex->getCode() == 500) {
$event->setError(Application::ERROR_EXCEPTION);
}
$event->setParam('exception', $ex);
}
}
示例4: format
/**
* @param \Zend\Mvc\MvcEvent $object
* @param array $options
*
* @return array
*/
public function format($object, array $options)
{
$data['route']['name'] = $object->getRouteMatch()->getMatchedRouteName();
$data['route']['params'] = $object->getRouteMatch()->getParams();
$parts = explode('/', $data['route']['name']);
$route = $object->getRouter();
$config = $object->getApplication()->getServiceManager()->get('config');
$config = isset($config['router']['routes']) ? $config['router']['routes'] : [];
while ($part = array_shift($parts)) {
$route->hasRoute($part) and $route = $route->getRoute($part);
isset($config[$part]) and $config = $config[$part];
}
$data['route']['class'] = get_class($route);
$data['route']['assembled'] = $route->getAssembledParams();
$data['event']['error'] = $object->getError();
$data['event']['name'] = $object->getName();
$controllers = [];
$definitions = [];
$title = '404 Error';
$subtitle = 'Unknown Error';
$context = null;
$manager = $object->getApplication()->getServiceManager()->get('ControllerLoader');
switch ($object->getError()) {
case Application::ERROR_CONTROLLER_NOT_FOUND:
$definitions = $config;
$title = $object->getControllerClass();
$subtitle = 'The requested controller cannot be found';
$controllers = $manager->getCanonicalNames();
array_pop($controllers);
// because the Sm add the wrong into the list
break;
case Application::ERROR_CONTROLLER_INVALID:
$title = $object->getControllerClass();
$subtitle = $object->getParam('exception')->getMessage();
break;
case Application::ERROR_CONTROLLER_CANNOT_DISPATCH:
$context = $this->getControllerContext($manager, $data['route']['params']);
$subtitle = 'The controller cannot dispatch the request';
$title = $data['route']['params']['controller'];
break;
}
$data['title'] = $title;
$data['subtitle'] = $subtitle;
$data['route']['definition'] = $definitions;
$data['controller']['names'] = $controllers;
$data['controller']['context'] = $context;
return $data;
}
示例5: onBootstrap
public function onBootstrap(MvcEvent $event)
{
$eventManager = $event->getApplication()->getEventManager();
$oauth2Closure = $event->getApplication()->getServiceManager()->get(\ZF\OAuth2\Service\OAuth2Server::class);
$logger = $event->getApplication()->getServiceManager()->get('logger');
$eventManager->attach(MvcAuthEvent::EVENT_AUTHENTICATION_POST, function (MvcAuthEvent $event) use($oauth2Closure) {
// Manipulating Identity Data
$identity = $event->getIdentity();
if (!!$identity) {
if ($identity instanceof AuthenticatedIdentity) {
$userData = $oauth2Closure()->getStorage('user_credentials')->getUser($identity->getName());
if (is_array($identity->getAuthenticationIdentity())) {
$userData = array_merge($userData, $identity->getAuthenticationIdentity());
}
$identity = new AuthenticatedIdentity($userData);
$event->setIdentity($identity);
}
//MvcEvent did not understand when manipulated MvcAuthEvent identity
$event->getMvcEvent()->setParam('ZF\\MvcAuth\\Identity', $identity);
}
return $event;
}, 900);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$event->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function (MvcEvent $event) use($logger) {
$problem = null;
if ($event->isError()) {
$exception = $event->getParam("exception");
// There are some other errors like that :
// "error-controller-cannot-dispatch",
// "error-controller-invalid",
// "error-controller-not-found",
// "error-router-no-match",
if ($event->getError() === 'error-controller-not-found') {
$problem = new ApiProblem(404, "Endpoint controller not found!");
} elseif ($event->getError() === 'error-router-no-match') {
$problem = new ApiProblem(404, "Not found!");
} elseif ($exception instanceof \Exception) {
$className = explode('\\', get_class($exception));
$problem = new ApiProblem($exception->getCode(), end($className) . ' error.');
$logger->err($exception->getMessage(), array('controller' => $event->getControllerClass()));
}
} else {
$problem = new ApiProblem(500, "Unknown Error!");
}
$response = new ApiProblemResponse($problem);
$event->stopPropagation();
return $response;
}, 9000);
}