本文整理汇总了PHP中Zend\Mvc\MvcEvent::getResponse方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getResponse方法的具体用法?PHP MvcEvent::getResponse怎么用?PHP MvcEvent::getResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_FINISH, function (MvcEvent $e) {
$config = $e->getApplication()->getServiceManager()->get('Config');
$routeMatch = $e->getRouteMatch();
if (!$routeMatch instanceof RouteMatch) {
return;
}
if (empty($config['page-cache'])) {
return;
}
if (empty($config['page-cache']['page-to-cache'])) {
return;
}
foreach ($config['page-cache']['page-to-cache'] as $match) {
if (strtolower($match['controller']) == strtolower($routeMatch->getParam('controller')) && strtolower($match['action']) == strtolower($routeMatch->getParam('action'))) {
$cache = $e->getApplication()->getServiceManager()->get('PageCache\\Model\\Cache');
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Cache-Created', date("D M j G:i:s T Y"));
$cache->addItem($this->getKey(), $e->getResponse()->toString());
break;
}
}
});
}
示例3: notFoundByRequestedCriteria
/**
* @return mixed
*/
public function notFoundByRequestedCriteria($criteriaErrors)
{
$zendResponse = $this->mvcEvent->getResponse();
$zendResponse->setStatusCode(404);
$zendResponse->getHeaders()->addHeaderLine("Content-Type", "application/json");
$zendResponse->setContent(json_encode($criteriaErrors));
return $zendResponse;
}
示例4: notFoundByRequestedCriteria
/**
* @return mixed
*/
public function notFoundByRequestedCriteria($criteriaErrors)
{
$zendResponse = $this->mvcEvent->getResponse();
$zendResponse->setStatusCode(404);
$this->viewModel->setVariable('message', 'The requested resource was not found by requested criteria');
$this->viewModel->setTemplate('error/404');
$this->mvcEvent->setResult($this->viewModel);
return $this->viewModel;
}
示例5: onError
public function onError(MvcEvent $event)
{
// Do nothing if no error or if response is not HTTP response
if (!($exception = $event->getParam('exception') instanceof UnauthorizedExceptionInterface) || !($response = $event->getResponse() instanceof HttpResponse)) {
return;
}
$response = $event->getResponse() ?: new HttpResponse();
$event->setResponse($response);
$event->setResult($response);
}
示例6: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$sm = $app->getServiceManager();
$list = $this->whitelist;
$auth = $sm->get('AuthService');
$e->getViewModel()->setVariable('hasIdentity', $auth->hasIdentity());
if ($auth->hasIdentity()) {
$e->getViewModel()->setVariable('currentUserId', $auth->getStorage()->read()->id);
} else {
$e->getViewModel()->setVariable('currentUserId', -1);
}
$eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) use($list, $auth) {
$match = $e->getRouteMatch();
// No route match, this is a 404
if (!$match instanceof RouteMatch) {
return;
} else {
// Route is whitelisted
$name = $match->getMatchedRouteName();
if (in_array($name, $list)) {
if ($auth->hasIdentity() && $name != 'login/process') {
$router = $e->getRouter();
$url = $router->assemble(array(), array('name' => 'home'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
} else {
return;
}
} else {
// User is authenticated
if ($auth->hasIdentity() && $name != 'auth/logout') {
// $id = $this->UserAuthentication()->getIdentity()->getId();
// var_dump(($auth->getIdentity()));
// exit;
// var_dump($name);exit;
return;
} else {
// Redirect to the user login page, as an example
$router = $e->getRouter();
$url = $router->assemble(array(), array('name' => 'login'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
}
}
}, -100);
}
示例7: displayError
protected function displayError($template, $status = 403)
{
$model = new ViewModel();
$model->setTerminal(false);
$model->setTemplate($template);
/** @var $response \Zend\Http\PhpEnvironment\Response */
$response = $this->_event->getResponse();
$response->setStatusCode($status);
$this->_event->setResponse($response);
$this->_event->setResult($model);
return;
}
示例8: preDispatch
/**
* @param MvcEvent $e
* @throws \Exception
* @throws \ServiceLocatorFactory\NullServiceLocatorException
*/
public function preDispatch(MvcEvent $e)
{
$application = $e->getApplication();
$sm = $application->getServiceManager();
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
$params = $matchedRoute->getParams();
$controller = $params['controller'];
if (!isset($controller)) {
return false;
}
$currentControllerNamespace = explode('\\', $controller);
if ($currentControllerNamespace[0] == 'Admin') {
$sl = ServiceLocatorFactory::getInstance();
$session = new SessionContainer();
$userDetails = $session->offsetGet('userDetails');
/* Check Admin Area login */
if (!$sl->get('AuthService')->hasIdentity() or $userDetails->sitename != $this->recoverSitename($sl)) {
$url = $e->getRouter()->assemble(array('action' => 'index'), array('name' => 'login'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
// Check ACL
$roles = (include __DIR__ . '/config/module.acl.roles.php');
foreach ($roles as $key => $value) {
if ($key == $matchedRoute->getMatchedRouteName()) {
if (isset($value['resources'])) {
$allowed = 0;
foreach ($value['resources'] as $resource) {
if ($userDetails->acl->hasResource($resource)) {
$allowed = 1;
}
}
/* No permissions, redirect... */
if ($allowed == 0) {
$url = $e->getRouter()->assemble(array('lang' => 'it'), array('name' => 'admin/not-authorized'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(401);
$response->sendHeaders();
exit;
}
}
}
}
}
}
示例9: onError
public function onError(MvcEvent $event)
{
// Do nothing if no error or if response is not HTTP response
if (!($exception = $event->getParam('exception') instanceof UnauthorizedExceptionInterface) || $event->getResult() instanceof HttpResponse || !($response = $event->getResponse() instanceof HttpResponse)) {
return;
}
$request = $event->getRequest() ?: new HttpRequest();
$response = $event->getResponse() ?: new HttpResponse();
// za json requeste vedno odgovorimo z json odgovorom nikoli z redirectom
$model = new JsonModel(['success' => false, 'errors' => [['message' => 'Unauthorized', 'code' => 'TIP-00403', 'severity' => 'error']]]);
$response->setStatusCode(403);
$event->setResponse($response);
$event->setResult($model);
}
示例10: routeEvent
/**
* Redirect old assets on sites that we do not control to new AWS urls
*/
public function routeEvent(MvcEvent $event)
{
$host = $event->getRequest()->getUri()->getHost();
if (!isset($this->domainRedirects[$host])) {
return null;
}
$event->getResponse()->setStatusCode(301);
/**
* @var $headers \Zend\Http\Headers
*/
$headers = $event->getResponse()->getHeaders();
$headers->addHeaderLine('location', $this->domainRedirects[$host] . $event->getRequest()->getUri()->getPath());
return $event->getResponse();
}
示例11: onRender
/**
* Method executed when the render event is triggered
*
* @param MvcEvent $e
* @return void
*/
public static function onRender(MvcEvent $e)
{
if ($e->getRequest() instanceof \Zend\Console\Request || $e->getResponse()->isOk() || $e->getResponse()->getStatusCode() == Response::STATUS_CODE_401) {
return;
}
$httpCode = $e->getResponse()->getStatusCode();
$sm = $e->getApplication()->getServiceManager();
$viewModel = $e->getResult();
$exception = $viewModel->getVariable('exception');
$model = new JsonModel(array('errorCode' => !empty($exception) ? $exception->getCode() : $httpCode, 'errorMsg' => !empty($exception) ? $exception->getMessage() : NULL));
$model->setTerminal(true);
$e->setResult($model);
$e->setViewModel($model);
$e->getResponse()->setStatusCode($httpCode);
}
示例12: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$this->initSession();
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$list = array();
$auth = $e->getApplication()->getServiceManager()->get("Zend\\Authentication\\AuthenticationService");
$eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) use($list, $auth) {
$match = $e->getRouteMatch();
// No route match, this is a 404
if (!$match instanceof RouteMatch) {
return;
}
// Route is whitelisted
$route = $match->getMatchedRouteName();
$params = $match->getParams();
$module = explode('/', $route);
/*
if($_SERVER['REMOTE_ADDR'] != '127.0.0.1' && $_SERVER['SERVER_PORT'] != '443') {
$url = 'Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header(str_replace( 'www.', '' , $url));
exit();
}*/
if (@$module[0] == 'login') {
if ($auth->hasIdentity()) {
$router = $e->getRouter();
$url = $router->assemble(array(), array('name' => 'messages'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
} else {
return;
}
}
if ($auth->hasIdentity()) {
return;
} else {
$router = $e->getRouter();
$url = $router->assemble(array(), array('name' => 'login'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
}, -100);
}
示例13: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'onViewHelper']);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function ($e) {
$controller = $e->getTarget();
$routeMatch = $e->getRouteMatch();
$apartmentId = $routeMatch->getParam('apartment_id', 0);
// get the apartment ID
if (method_exists($controller, 'setApartmentID')) {
$serviceManager = $e->getApplication()->getServiceManager();
$dbAdapter = $serviceManager->get('Zend\\Db\\Adapter\\Adapter');
$apartmentExistValidator = new RecordExists(['adapter' => $dbAdapter, 'table' => DbTables::TBL_APARTMENTS, 'field' => 'id']);
if (!$apartmentExistValidator->isValid($apartmentId) && $apartmentId != 0) {
$url = $e->getRouter()->assemble(array('controller' => 'apartment', 'action' => 'search'), ['name' => 'apartments']);
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
return $response;
}
$controller->setApartmentID($apartmentId);
}
}, 100);
}
示例14: onRenderError
/**
* Handle rendering errors
*
* Rendering errors are usually due to trying to render a template in
* the PhpRenderer, when we have no templates.
*
* As such, report as an unacceptable response.
*
* @param MvcEvent $e
*/
public function onRenderError(MvcEvent $e)
{
$response = $e->getResponse();
$status = 406;
$title = 'Not Acceptable';
$describedBy = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html';
$detail = 'Your request could not be resolved to an acceptable representation.';
$details = false;
$exception = $e->getParam('exception');
if ($exception instanceof \Exception && !$exception instanceof ViewExceptionInterface) {
$code = $exception->getCode();
if ($code >= 100 && $code <= 600) {
$status = $code;
} else {
$status = 500;
}
$title = 'Unexpected error';
$detail = $exception->getMessage();
$details = ['code' => $exception->getCode(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString()];
}
$payload = ['status' => $status, 'title' => $title, 'describedBy' => $describedBy, 'detail' => $detail];
if ($details && $this->displayExceptions) {
$payload['details'] = $details;
}
$response->getHeaders()->addHeaderLine('content-type', 'application/problem+json');
$response->setStatusCode($status);
$response->setContent(json_encode($payload));
$e->stopPropagation();
}
示例15: render
/**
* Render the view
*
* @param MvcEvent $e
* @return Response
*/
public function render(MvcEvent $e)
{
$result = $e->getResult();
if ($result instanceof Response) {
return $result;
}
// Martial arguments
$request = $e->getRequest();
$response = $e->getResponse();
$viewModel = $e->getViewModel();
if (!$viewModel instanceof ViewModel) {
return;
}
$view = $this->view;
$view->setRequest($request);
$view->setResponse($response);
try {
$view->render($viewModel);
} catch (\Exception $ex) {
if ($e->getName() === MvcEvent::EVENT_RENDER_ERROR) {
throw $ex;
}
$application = $e->getApplication();
$events = $application->getEventManager();
$e->setError(Application::ERROR_EXCEPTION)->setParam('exception', $ex);
$events->trigger(MvcEvent::EVENT_RENDER_ERROR, $e);
}
return $response;
}