本文整理汇总了PHP中Zend\Mvc\MvcEvent::getError方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getError方法的具体用法?PHP MvcEvent::getError怎么用?PHP MvcEvent::getError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processUnAuth
public function processUnAuth(MvcEvent $e)
{
//$d = new \Zend\Debug\Debug(); $d->dump($e->getError());
$error = $e->getError();
if ($e->getError() === 'error-route-unauthorized') {
$sm = $e->getApplication()->getServiceManager();
$conf = $sm->get('IdAuth\\Config');
if ($conf['settings']['useDifferentLayoutForUnAuth']) {
$user = new Forms\Login();
$builder = new \Zend\Form\Annotation\AnnotationBuilder();
$loginForm = $builder->createForm($user);
$view = $e->getViewModel();
$view->loginForm = $loginForm;
$view->setTemplate('idauth/locked');
$view->error = $error;
$auth = $sm->get('IdAuthService');
$hasIdentity = $auth->hasIdentity();
if ($hasIdentity) {
$view->gravatarEmail = $auth->getIdentity()->getEmail();
} else {
$view->gravatarEmail = null;
}
$view->hasIdentity = $auth->hasIdentity();
$view->identity = $auth->getIdentity();
$view->route = $e->getRouteMatch()->getMatchedRouteName();
}
}
}
示例2: verifyIsError
/**
* Verify is error
*
* @throws \LogicException
*/
private function verifyIsError()
{
$error = $this->mvcEvent->getError();
if (!$error) {
throw new \LogicException("This listener is only meant to be called on errors");
}
}
示例3: dispatchError
/**
* @param MvcEvent $event
*/
public function dispatchError(MvcEvent $event)
{
$exception = $event->getParam('exception');
while ($exception) {
$this->logger->critical('EVENT_DISPATCH_ERROR: ' . $exception->getMessage(), $exception->getTrace());
$exception = $exception->getPrevious();
}
if ($event->isError() == true && $event->getError() == Application::ERROR_EXCEPTION) {
$this->logger->critical('EVENT_DISPATCH_ERROR: ' . $event->getError());
}
}
示例4: 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);
}
示例5: 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;
}
示例6: onDispatchError
/**
* Callback used when a dispatch error occurs. Modifies the
* response object with an according error if the application
* event contains an exception related with authorization.
*
* @param MvcEvent $event
*
* @return void
*/
public function onDispatchError(MvcEvent $event)
{
// Do nothing if the result is a response object
$result = $event->getResult();
$response = $event->getResponse();
if ($result instanceof Response || $response && !$response instanceof HttpResponse) {
return;
}
// Common view variables
$viewVariables = array('error' => $event->getParam('error'), 'identity' => $event->getParam('identity'));
switch ($event->getError()) {
case Application::ERROR_EXCEPTION:
if (!$event->getParam('exception') instanceof UnAuthorizedException) {
return;
}
$viewVariables['reason'] = $event->getParam('exception')->getMessage();
$viewVariables['error'] = 'error-unauthorized';
break;
default:
/*
* do nothing if there is no error in the event or the error
* does not match one of our predefined errors (we don't want
* our 403 template to handle other types of errors)
*/
return;
}
$model = new ViewModel($viewVariables);
$response = $response ?: new HttpResponse();
$model->setTemplate($this->getTemplate());
$event->getViewModel()->addChild($model);
$response->setStatusCode(403);
$event->setResponse($response);
}
示例7: handleControllerNotFoundAndControllerInvalidAndRouteNotFound
public function handleControllerNotFoundAndControllerInvalidAndRouteNotFound(MvcEvent $e)
{
$error = $e->getError();
$typeErreur = null;
if ($error == Application::ERROR_CONTROLLER_NOT_FOUND) {
//there is no controller named $e->getRouteMatch()->getParam('controller')
$typeErreur = 2;
}
if ($error == Application::ERROR_CONTROLLER_INVALID) {
//the controller doesn't extends AbstractActionController
$typeErreur = 3;
}
if ($error == Application::ERROR_ROUTER_NO_MATCH) {
// the url doesn't match route, for example, there is no /foo literal of route
$typeErreur = 4;
}
if ($typeErreur != null) {
$url = $e->getRouter()->assemble(array('erreur' => 'erreur'), array('name' => 'home', 'query' => array('erreur' => $typeErreur)));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
return $response;
}
}
示例8: keepLayout
/**
* Keep the same layout if user access forbidden page
*
* @param MvcEvent $e
*/
public function keepLayout(MvcEvent $e)
{
$error = $e->getError();
if (empty($error) || in_array($error, [AclListener::ERROR_FORBIDDEN, AbstractControllerGuard::ERROR_FORBIDDEN])) {
$this->updateLayout($e);
}
}
示例9: onDispatchError
public function onDispatchError(MvcEvent $event)
{
switch ($event->getError()) {
case AccessService::ACCESS_DENIED:
$statusCode = 403;
break;
case AccessService::ACCESS_UNAUTHORIZED:
$statusCode = 401;
break;
default:
// do nothing if this is a different kind of error we should not trap
return;
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$viewModel = new JsonModel();
} else {
$viewModel = new ViewModel();
$viewModel->setTemplate('user/' . $statusCode);
}
$viewModel->setVariables($event->getParams());
$response = $event->getResponse() ?: new Response();
$response->setStatusCode($statusCode);
$event->setViewModel($viewModel);
$event->setResponse($response);
}
示例10: createCustomEvent
/**
* Create a custom event in Zend Server
*
* @param MvcEvent $e
* @return void
*/
public function createCustomEvent(MvcEvent $e)
{
// Do nothing if no error in the event
$error = $e->getError();
if (empty($error)) {
return;
}
// Do nothing if the result is a response object
$result = $e->getResult();
if ($result instanceof Response) {
return;
}
switch ($error) {
case Application::ERROR_CONTROLLER_NOT_FOUND:
case Application::ERROR_CONTROLLER_INVALID:
case Application::ERROR_ROUTER_NO_MATCH:
// Specifically not handling these
return;
case Application::ERROR_EXCEPTION:
default:
if (!empty($e)) {
$exception = $e->getParam('exception');
/* @var $exception \Exception */
$message = 'An error occurred during execution: ' . $exception->getMessage();
if ($this->isCustomEventByRuleIsEnabled()) {
zend_monitor_custom_event_ex('Zend Framework Exception', $message, 'Zend Framework Exception Rule');
} elseif ($this->isCustomEventIsEnabled) {
zend_monitor_custom_event('Zend Framework Exception', $message);
}
}
}
}
示例11: onDispatchError
/**
* @param MvcEvent $event
*/
public function onDispatchError(MvcEvent $event)
{
if (Application::ERROR_ROUTER_NO_MATCH != $event->getError()) {
// ignore other than 'no route' errors
return;
}
// get URI stripped of a base URL
$request = $event->getRequest();
$uri = str_replace($request->getBaseUrl(), '', $request->getRequestUri());
// try get image ID from URI
$id = $this->manager->matchUri($uri);
if (!$id) {
// abort if URI does not match
return;
}
// try get image from repository
$image = $this->repository->find($id);
if (!$image) {
// abort if image does not exist
return;
}
// store image
$this->manager->store($image);
// return image in response as a stream
$headers = new Headers();
$headers->addHeaders(['Content-Type' => $image->getType(), 'Content-Length' => $image->getLength()]);
$response = new Stream();
$response->setStatusCode(Response::STATUS_CODE_200);
$response->setStream($image->getResource());
$response->setStreamName($image->getName());
$response->setHeaders($headers);
$event->setResponse($response);
}
示例12: __invoke
public function __invoke(MvcEvent $event)
{
$model = $event->getResult();
if (!$model instanceof ViewModel) {
return;
}
if (strpos($model->getTemplate(), 'error') === false) {
return;
}
$result = $event->getResult();
$error = $event->getError();
$layout = new ViewModel();
$layout->setTemplate('layout/layout');
$content = new ViewModel();
if ($error == 'error-exception') {
$content->setVariable('reason', 'The site seems to be experiencing problems, please try again later');
$content->setTemplate('error/knc-exception');
} else {
$content->setVariable('reason', 'The site cannot find the url in the address bar');
$content->setTemplate('error/knc-error');
}
$layout->addChild($content);
$layout->setTerminal(true);
$event->setViewModel($layout);
$event->setResult($layout);
return false;
}
示例13: onDispatchError
/**
* Handles redirects in case of dispatch errors caused by unauthorized access
*
* @param \Zend\Mvc\MvcEvent $event
*/
public function onDispatchError(MvcEvent $event)
{
// Do nothing if the result is a response object
$result = $event->getResult();
$routeMatch = $event->getRouteMatch();
$response = $event->getResponse();
$router = $event->getRouter();
$error = $event->getError();
$url = $this->redirectUri;
if ($result instanceof Response || !$routeMatch || $response && !$response instanceof Response || !(Route::ERROR === $error || Controller::ERROR === $error || Application::ERROR_EXCEPTION === $error && $event->getParam('exception') instanceof UnAuthorizedException)) {
return;
}
// if application needs install
if (AppGuard::ERROR === $error && $event->getParam('exception') instanceof NeedsInstallException) {
die('died here');
$this->redirectRoute = 'zfmuscle/install';
}
if (null === $url) {
$url = $router->assemble(array(), array('name' => $this->redirectRoute));
}
$response = $response ?: new Response();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$event->setResponse($response);
}
示例14: prepareExceptionViewModel
public function prepareExceptionViewModel(MvcEvent $event)
{
// Do nothing if no error in the event
$error = $event->getError();
if (empty($error)) {
return;
}
// Do nothing if the result is a response object
$result = $event->getResult();
if ($result instanceof Response) {
return;
}
switch ($error) {
case Application::ERROR_CONTROLLER_NOT_FOUND:
case Application::ERROR_CONTROLLER_INVALID:
case Application::ERROR_ROUTER_NO_MATCH:
// Specifically not handling these
return;
case Application::ERROR_EXCEPTION:
default:
$exception = $event->getParam('exception');
if ($exception) {
$response = $event->getResponse();
if (!$response || $response->getStatusCode() === 200) {
header('HTTP/1.0 500 Internal Server Error', true, 500);
}
ob_clean();
$this->run->handleException($event->getParam('exception'));
}
break;
}
}
示例15: prepareException
/**
* Whoops handle exceptions
* @param MvcEvent $e
*/
public function prepareException(MvcEvent $e)
{
if ($e->getRequest() instanceof Request) {
$error = $e->getError();
if (!empty($error) && !$e->getResult() instanceof Response) {
switch ($error) {
case Application::ERROR_CONTROLLER_NOT_FOUND:
case Application::ERROR_CONTROLLER_INVALID:
case Application::ERROR_ROUTER_NO_MATCH:
// Specifically not handling these
return;
case Application::ERROR_EXCEPTION:
default:
/** @var Exception $exception */
$exception = $e->getParam('exception');
// Filter exceptions that we must except
foreach ($this->whoopsConfig['blacklist'] as $except) {
if ($exception instanceof $except) {
return;
}
}
if ($this->whoopsConfig['handler']['options_type'] === 'prettyPage') {
$response = $e->getResponse();
if (!$response || $response->getStatusCode() === 200) {
header('HTTP/1.0 500 Internal Server Error', true, 500);
}
ob_clean();
}
$this->run->handleException($e->getParam('exception'));
break;
}
}
}
}