本文整理汇总了PHP中Zend\Mvc\MvcEvent::getResult方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getResult方法的具体用法?PHP MvcEvent::getResult怎么用?PHP MvcEvent::getResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getResult方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onBootstrap
/**
* Boostrap
*
* @param MvcEvent $mvcEvent
*/
public function onBootstrap(MvcEvent $mvcEvent)
{
$eventManager = $mvcEvent->getApplication()->getEventManager();
/**
* Disable Layout on Error
*/
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($mvcEvent) {
/**
* @var MvcEvent $mvcEvent
*/
$mvcEvent->getResult()->setTerminal(true);
});
$sharedEvents = $eventManager->getSharedManager();
/**
* Disable Layout in ViewModel
*/
$sharedEvents->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function ($mvcEvent) {
/**
* @var MvcEvent $mvcEvent
*/
$result = $mvcEvent->getResult();
if ($result instanceof ViewModel) {
$result->setTerminal(true);
}
});
$mvcEvent->getApplication()->getEventManager()->getSharedManager()->attach('Mp3\\Controller\\SearchController', 'Mp3Help', function ($event) use($mvcEvent) {
/**
* @var MvcEvent $event
*/
echo $mvcEvent->getApplication()->getServiceManager()->get('Mp3\\Service\\Search')->help($event->getParam('help'));
});
}
示例2: prepareViewModel
public function prepareViewModel(MvcEvent $event, $action)
{
if ($event->getTarget()->forward()->getNumNestedForwards() > 0) {
return $event->getResult();
}
$result = $event->getResult();
$response = $event->getResponse();
$response->setStatusCode($result->getStatusCode());
$response->getHeaders()->addHeaders($result->getHeaders());
$controller = $event->getTarget();
$viewModel = $controller->acceptableViewModelSelector($controller->getOptions()->getAcceptCriteria());
if ($vars = $result->getSerializedModel()) {
$viewModel->setVariables($vars);
}
//set the template
if ($viewModel instanceof JsonModel && count($viewModel->getVariables()) == 0) {
if ($response->getStatusCode() == 200) {
$response->setStatusCode(204);
}
return $response;
} elseif ($viewModel->getTemplate() == null) {
$viewModel->setTemplate($controller->getOptions()->getTemplates()[$action]);
}
$event->setResult($viewModel);
return $viewModel;
}
示例3: disableLayouts
public function disableLayouts(MvcEvent $e)
{
$latteResolver = $this->sm->get('Zf2Latte\\LatteResolver');
$viewModel = $e->getResult();
if ($latteResolver->resolve($viewModel->getTemplate())) {
$e->getResult()->setTerminal(true);
}
}
示例4: onRender
/**
* Renders ZF1 response into a ZF2 response
*
* @param \Zend\Mvc\MvcEvent $e
* @return \Zend\Stdlib\ResponseInterface|null
*/
public function onRender(MvcEvent $e)
{
if (!$e->getResult() instanceof \Zend_Controller_Response_Abstract) {
return;
}
$response = $e->getResponse();
$this->renderIntoResponse($response, $e->getResult());
$e->setResult($response);
return $response;
}
示例5: addLocationHeader
protected function addLocationHeader(MvcEvent $event)
{
$options = $event->getTarget()->getOptions();
if ($property = $options->getProperty()) {
$result = $event->getResult();
$createdDocument = $result->getModel();
$result->addHeader(Location::fromString('Location: ' . $event->getRequest()->getUri()->getPath() . '/' . $options->getModelManager()->getClassMetadata(get_class($createdDocument))->getFieldValue($createdDocument, $property)));
}
return $event->getResult();
}
示例6: flush
public function flush(MvcEvent $event)
{
if ($event->getTarget()->forward()->getNumNestedForwards() > 0) {
return $event->getResult();
}
$options = $event->getTarget()->getOptions();
$options->getModelManager()->flush();
if (!($flushExceptions = $options->getExceptionSubscriber()->getFlushExceptions())) {
return $event->getResult();
} else {
return $this->prepareExceptions($flushExceptions, $options->getExceptionSerializer());
}
}
示例7: testPopulateResponse
public function testPopulateResponse()
{
$exception = new Exception\Client\BadRequestException('Validation errors', ['email' => 'invalid']);
$this->event->setParam('exception', $exception);
$this->httpExceptionListener->onDispatchError($this->event);
$response = $this->event->getResponse();
$expectedContent = ['status_code' => 400, 'message' => 'Validation errors', 'errors' => ['email' => 'invalid']];
$this->assertNotSame($this->response, $response, 'Assert response is replaced');
$this->assertInstanceOf(Response::class, $this->event->getResponse());
$this->assertInstanceOf(Response::class, $this->event->getResult());
$this->assertEquals($expectedContent, json_decode($this->event->getResponse()->getContent(), true));
$this->assertTrue($this->event->propagationIsStopped());
}
示例8: unserializeSingle
public function unserializeSingle(MvcEvent $event, $mode)
{
if (count($event->getParam('deeperResource')) > 0 || ($result = $event->getResult())) {
return $event->getResult();
}
$data = $event->getParam('data');
$id = $event->getParam('id');
$options = $event->getTarget()->getOptions();
if ($property = $options->getProperty()) {
$data[$property] = $id;
}
$result = new Result($event->getTarget()->getOptions()->getManifest()->getServiceManager()->get('unserializer')->fromArray($data, $event->getTarget()->getOptions()->getClass(), $event->getParam('document'), $mode));
$event->setResult($result);
return $result;
}
示例9: onInvokation
protected function onInvokation(MvcEvent $e, $error = false)
{
$viewModel = $e->getResult();
$isJsonModel = $viewModel instanceof JsonModel;
$routeMatch = $e->getRouteMatch();
if ($routeMatch && $routeMatch->getParam('forceJson', false) || $isJsonModel || "json" == $e->getRequest()->getQuery('format') || "json" == $e->getRequest()->getPost('format')) {
if (!$isJsonModel) {
$model = new JsonModel();
if ($error) {
$model->status = 'error';
$model->message = $viewModel->message;
if ($viewModel->display_exceptions) {
if (isset($viewModel->exception)) {
$model->exception = $viewModel->exception->getMessage();
}
}
} else {
$model->setVariables($viewModel->getVariables());
}
$viewModel = $model;
$e->setResult($model);
$e->setViewModel($model);
}
$viewModel->setTerminal(true);
$strategy = new \Zend\View\Strategy\JsonStrategy(new \Zend\View\Renderer\JsonRenderer());
$view = $e->getApplication()->getServiceManager()->get('ViewManager')->getView();
$view->addRenderingStrategy(array($strategy, 'selectRenderer'), 10);
$view->addResponseStrategy(array($strategy, 'injectResponse'), 10);
}
}
示例10: prepareForbiddenViewModel
/**
* Create and return a 403 view model
*
* @param MvcEvent $event
* @return void
*/
public function prepareForbiddenViewModel(MvcEvent $event)
{
if ($event->getRequest() instanceof \Zend\Console\Request) {
// CLI mode
return;
}
$vars = $event->getResult();
if ($vars instanceof Response) {
// Already have a response as the result
return;
}
$response = $event->getResponse();
if ($response->getStatusCode() != 403) {
// Only handle 403 responses
return;
}
if (!$vars instanceof ViewModel) {
$model = new ViewModel();
if (is_string($vars)) {
$model->setVariable('message', $vars);
} else {
$model->setVariable('message', 'Page is forbidden.');
}
} else {
$model = $vars;
if ($model->getVariable('message') === null) {
$model->setVariable('message', 'Page is forbidden.');
}
}
$model->setTemplate($this->getForbiddenTemplate());
$event->setResult($model);
}
示例11: injectTemplate
/**
* Inject a template into the view model, if none present
*
* Template is derived from the controller found in the route match, and,
* optionally, the action, if present.
*
* @param MvcEvent $e
* @return void
*/
public function injectTemplate(MvcEvent $e)
{
$model = $e->getResult();
if (!$model instanceof ViewModel) {
return;
}
$template = $model->getTemplate();
if (!empty($template)) {
return;
}
$routeMatch = $e->getRouteMatch();
$controller = $e->getTarget();
if (is_object($controller)) {
$controller = get_class($controller);
}
if (!$controller) {
$controller = $routeMatch->getParam('controller', '');
}
$module = $this->deriveModuleNamespace($controller);
$controller = $this->deriveControllerClass($controller);
$template = $this->inflectName($module);
if (!empty($template)) {
$template .= '/';
}
$template .= $this->inflectName($controller);
$action = $routeMatch->getParam('action');
if (null !== $action) {
$template .= '/' . $this->inflectName($action);
}
$model->setTemplate($template);
}
示例12: onError
/**
* @private
* @param MvcEvent $event
* @return void
*/
public function onError(MvcEvent $event)
{
// Do nothing if no error or if response is not HTTP response
if (!$event->getParam('exception') instanceof UnauthorizedExceptionInterface || $event->getResult() instanceof HttpResponse || !$event->getResponse() instanceof HttpResponse) {
return;
}
$router = $event->getRouter();
if ($this->authenticationService->hasIdentity()) {
if (!$this->options->getRedirectWhenConnected()) {
return;
}
$redirectRoute = $this->options->getRedirectToRouteConnected();
} else {
$redirectRoute = $this->options->getRedirectToRouteDisconnected();
}
$uri = $router->assemble([], ['name' => $redirectRoute]);
if ($this->options->getAppendPreviousUri()) {
$redirectKey = $this->options->getPreviousUriQueryKey();
$previousUri = $event->getRequest()->getUriString();
$uri = $router->assemble([], ['name' => $redirectRoute, 'query' => [$redirectKey => $previousUri]]);
}
$response = $event->getResponse() ?: new HttpResponse();
$response->getHeaders()->addHeaderLine('Location', $uri);
$response->setStatusCode(302);
$event->setResponse($response);
$event->setResult($response);
}
示例13: onDispatchError
/**
* Get the exception and optionally set status code, reason message and additional errors
*
* @internal
* @param MvcEvent $event
* @return void
*/
public function onDispatchError(MvcEvent $event)
{
$exception = $event->getParam('exception');
if (isset($this->exceptionMap[get_class($exception)])) {
$exception = $this->createHttpException($exception);
}
// We just deal with our Http error codes here !
if (!$exception instanceof HttpExceptionInterface || $event->getResult() instanceof HttpResponse) {
return;
}
// We clear the response for security purpose
$response = new HttpResponse();
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$exception->prepareResponse($response);
// NOTE: I'd like to return a JsonModel instead, and let ZF handle the request, but I couldn't make
// it work because for unknown reasons, the Response get replaced "somewhere" in the MVC workflow,
// so the simplest is simply to do that
$content = ['status_code' => $response->getStatusCode(), 'message' => $response->getReasonPhrase()];
if ($errors = $exception->getErrors()) {
$content['errors'] = $errors;
}
$response->setContent(json_encode($content));
$event->setResponse($response);
$event->setResult($response);
$event->stopPropagation(true);
}
示例14: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$app_config = $sm->get('config');
$app_options = $app_config['app_options'];
if (array_key_exists('recover_from_fatal', $app_options) && $app_options['recover_from_fatal']) {
$redirect_url = $app_options['redirect_url'];
$callback = null;
if (array_key_exists('fatal_errors_callback', $app_options) && $app_options['fatal_errors_callback']) {
$callback = $app_options['fatal_errors_callback'];
}
register_shutdown_function(array('Application\\Module', 'handleFatalPHPErrors'), $redirect_url, $callback);
}
set_error_handler(array('Application\\Module', 'handlePHPErrors'));
foreach ($app_options['php_settings'] as $key => $value) {
ini_set($key, $value);
}
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$logger = $sm->get('Logger');
$eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, function (MvcEvent $e) use($logger) {
$logger->info('An Exception has occurred. ' . $e->getResult()->exception->getMessage());
}, -200);
}
示例15: setTemplate
public function setTemplate(MvcEvent $e)
{
$result = $e->getResult();
$router = $e->getRouteMatch();
$params = $router->getParams();
if (isset($params['__NAMESPACE__']) && isset($params['__CONTROLLER__']) && isset($params['action'])) {
$controllerClass = $params['__NAMESPACE__'];
$moduleNamespace = substr($controllerClass, strpos($controllerClass, '\\'));
$moduleNamespace = substr($moduleNamespace, 1);
$moduleNamespace = substr($moduleNamespace, 0, strpos($moduleNamespace, '\\Controller'));
$templatePath = __DIR__;
$config = $e->getApplication()->getServiceManager()->get('config');
if (isset($config['view_manager']['template_map'])) {
$moduleNamespace = strtolower($moduleNamespace . '/' . $params['__CONTROLLER__']);
$templatePath = $templatePath . '/view/' . $moduleNamespace . '/' . $params['action'] . '.phtml';
$moduleNamespace = str_replace('\\', '/', $moduleNamespace);
$template = $moduleNamespace . '/' . $params['action'];
$config['view_manager']['template_map'][$template] = $templatePath;
$result->setTemplate($template);
}
//set Title;
if (isset($config['view_manager']['siteName'])) {
$siteName = $config['view_manager']['siteName'];
} else {
$siteName = $moduleNamespace;
}
$viewHelperManager = $e->getApplication()->getServiceManager()->get('viewHelperManager');
$headTitle = $viewHelperManager->get('headTitle');
$headTitle->setSeparator(' - ');
$headTitle->append($params['action']);
$headTitle->append($siteName);
}
}