本文整理汇总了PHP中Zend\Mvc\MvcEvent::stopPropagation方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::stopPropagation方法的具体用法?PHP MvcEvent::stopPropagation怎么用?PHP MvcEvent::stopPropagation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::stopPropagation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handleRestApiError
public function handleRestApiError(MvcEvent $e)
{
if (array_key_exists('exception', $e->getParams())) {
$exception = $e->getParams()['exception'];
if ($exception instanceof RestApiException) {
$e->stopPropagation();
$response = $e->getResponse();
$response->setStatusCode('400');
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$adapter = new Json();
$errorData = $exception->getErrorData();
$original = $exception->getOriginal();
$count = 0;
while ($original instanceof \Exception) {
$errorData['previous-' . $count] = $original->getMessage() . '/' . $original->getFile() . '/' . $original->getLine();
$original = $original->getPrevious();
$count++;
}
$response->setContent($adapter->serialize($errorData));
$response->sendHeaders();
$response->sendContent();
exit;
}
}
}
示例2: 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);
}
示例3: checkDomain
/**
* Check the domain is a known domain for the CMS. If not the primary, it will
* redirect the user to the primary domain. Useful for multiple domain sites.
*
* @param MvcEvent $event Zend MVC Event
*
* @return null|Response
*/
public function checkDomain(MvcEvent $event)
{
if ($this->isConsoleRequest()) {
return null;
}
$currentDomain = $this->siteService->getCurrentDomain();
$site = $this->siteService->getCurrentSite($currentDomain);
$redirectUrl = $this->domainRedirectService->getSiteNotAvailableRedirectUrl($site);
if (!$site->isSiteAvailable() && empty($redirectUrl)) {
$response = new Response();
$response->setStatusCode(404);
$event->stopPropagation(true);
return $response;
}
if ($redirectUrl) {
$response = new Response();
$response->setStatusCode(302);
$response->getHeaders()->addHeaderLine('Location', '//' . $redirectUrl);
$event->stopPropagation(true);
return $response;
}
$redirectUrl = $this->domainRedirectService->getPrimaryRedirectUrl($site);
if ($redirectUrl) {
$response = new Response();
$response->setStatusCode(302);
$response->getHeaders()->addHeaderLine('Location', '//' . $redirectUrl);
$event->stopPropagation(true);
return $response;
}
return null;
}
示例4: postRoute
public function postRoute(MvcEvent $event)
{
// we want to test only parsing of arguments and routing.
// No real dispatching is needed here
$event->stopPropagation(true);
return $event->getResponse()->setContent("stopped");
}
示例5: renderRequest
/**
* @param MvcEvent $event
*/
public function renderRequest(MvcEvent $event)
{
/** @var Response $zendResponse */
$zendResponse = $event->getResponse();
$zendResponse->send();
$event->stopPropagation();
}
示例6: checkAuthentication
public function checkAuthentication(MvcEvent $event)
{
if ($event->getRequest() instanceof ConsoleResquest) {
return;
}
$routename = $event->getRouteMatch()->getMatchedRouteName();
// TODO mettre en conf
if ($routename == 'zfcuser/login' || $routename == 'zfcuser/logout' || $routename == 'execution') {
return;
}
$zfcUser = $this->getServiceLocator()->get('zfcuser_auth_service');
$config = $this->getServiceLocator()->get('Config');
$authorized = false;
if ($zfcUser->hasIdentity()) {
$authorized = $this->getFileAuthService()->checkAutorisation($zfcUser->getIdentity()->getUserName(), $routename);
}
if (!$zfcUser->hasIdentity() || true !== $authorized) {
$url = $event->getRouter()->assemble(array(), array('name' => 'zfcuser/login'));
$response = $event->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
$stopCallBack = function ($event) use($response) {
$event->stopPropagation();
return $response;
};
$event->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, $stopCallBack, -10000);
return $response;
}
}
示例7: doAuthentication
public function doAuthentication(MvcEvent $event)
{
if (!$event->getRequest() instanceof Request) {
return;
}
$application = $event->getApplication();
$sm = $application->getServiceManager();
$match = $event->getRouteMatch();
$controller = $match->getParam('controller');
$action = $match->getParam('action');
$plugin = $sm->get('ControllerPluginManager')->get('IsAllowed');
$hasIdentity = $plugin->getIdentity();
if (!$plugin->isAllowed($controller, $action)) {
$router = $event->getRouter();
$url = $router->assemble([], ['name' => 'guest' === $hasIdentity->getRoleId() ? 'user' : 'home']);
$response = $event->getResponse();
$response->setStatusCode(302);
//redirect to login route...
// change with header('location: '.$url); if code below not working
$response->getHeaders()->addHeaderLine('Location', $url);
$event->stopPropagation();
return $response;
}
return;
}
示例8: 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();
}
示例9: onPreBootstrap
public function onPreBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
$services = $application->getServiceManager();
$events = $application->getEventManager();
$config = $services->has('Config') ? $services->get('Config') : [];
// Check Database connectivity
if ($services->has('Zend\\Db\\Adapter\\Adapter')) {
$adapter = $services->get('Zend\\Db\\Adapter\\Adapter');
try {
$adapter->getDriver()->getConnection()->connect();
} catch (\Exception $ex) {
$viewManagerConfig = $config['view_manager'] ? $config['view_manager'] : [];
$response = $e->getResponse();
$viewModel = $e->getViewModel();
$template = isset($viewManagerConfig['service_unavailable_template']) ? $viewManagerConfig['service_unavailable_template'] : '503';
$viewModel->setTemplate($template);
if ($response instanceof HttpResponse) {
$response->setStatusCode(503);
} elseif ($response instanceof ConsoleModel) {
$response->setErrorLevel(1);
} else {
echo "Service Unavailable.";
exit(1);
}
$event = $e;
$event->setResponse($response);
$event->setTarget($application);
$events->trigger(MvcEvent::EVENT_RENDER, $event);
$events->trigger(MvcEvent::EVENT_FINISH, $event);
$e->stopPropagation(true);
exit(1);
}
}
// Maintenance mode
// Session Management
if (!$services->has('Zend\\Session\\Config\\ConfigInterface')) {
if (isset($config['session_config'])) {
$services->setFactory('Zend\\Session\\ConfigInterface', 'Zend\\Session\\Service\\SessionConfigFactory');
}
}
if (!$services->has('Zend\\Session\\Storage\\StorageInterface')) {
if (isset($config['session_storage'])) {
$services->setFactory('Zend\\Session\\StorageInterface', 'Zend\\Session\\Service\\StorageFactory');
}
}
if (!$services->has('Zend\\Session\\SaveHandler\\SaveHandlerInterface')) {
if ($services->has('Zend\\Db\\Adapter\\Adapter')) {
$adapter = $services->get('Zend\\Db\\Adapter\\Adapter');
$tableGateway = new TableGateway('sessions', $adapter);
$options = new DbTableGatewayOptions();
$sessionHandler = new DbTableGateway($tableGateway, $options);
$services->setService('Zend\\Session\\SaveHandler\\SaveHandlerInterface', $sessionHandler);
}
}
if (!$services->has('Zend\\Session\\ManagerInterface')) {
$services->setFactory('Zend\\Session\\ManagerInterface', 'Zend\\Session\\Service\\SessionManagerFactory');
}
}
示例10: sendToUrl
protected function sendToUrl(MvcEvent $e, $url)
{
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode($this->responseCode);
$response->sendHeaders();
return $e->stopPropagation();
}
示例11: terminateEvent
protected function terminateEvent(MvcEvent $event, $error, \Exception $exception)
{
$eventManager = $event->getApplication()->getEventManager();
$event->setError($error);
$event->setParam('exception', $exception);
$event->stopPropagation(true);
$eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
}
示例12: handleLocale
/**
* Handle locale
*/
public function handleLocale()
{
/** @var PhpRequest $request */
$request = $this->mvcEvent->getRequest();
$response = $this->mvcEvent->getResponse();
$moduleOptions = $this->getModuleOptions();
if (!$request instanceof PhpRequest || !$moduleOptions->isMultiLanguage()) {
return null;
}
/** @var LocaleNameParserService $localeNameParserService */
$localeNameParserService = $this->mvcEvent->getApplication()->getServiceManager()->get(LocaleNameParserService::class);
try {
$localeFromHost = $localeNameParserService->getLocaleFromHost();
$this->setAppLocale($localeFromHost);
if (!$moduleOptions->isMappedDomainRedirectable()) {
return null;
}
} catch (LocaleNotFoundException $exception) {
//no locale in host? Let's fond them somewhere else!
$localeFromHost = null;
}
try {
$localeFromPath = $localeNameParserService->getLocaleFromUriPath();
$this->setAppLocale($localeFromPath);
return null;
} catch (LocaleNotFoundException $exception) {
$localeFromPath = null;
}
if (is_null($localeFromPath) && !is_null($localeFromHost)) {
return null;
}
//use browser locale
try {
$locale = $localeNameParserService->getLocaleFromBrowser();
} catch (LocaleNotFoundException $exception) {
$locale = $localeNameParserService->getDefaultLocale();
}
/** @var PhpResponse $response */
$url = $localeNameParserService->getUrlForLocale($locale);
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(PhpResponse::STATUS_CODE_302);
$response->sendHeaders();
$this->mvcEvent->stopPropagation(true);
return $response;
}
示例13: determineTtl
/**
* @inheritDoc
*/
public function determineTtl(MvcEvent $e)
{
$viewModel = $e->getViewModel();
$esiOptions = (array) $viewModel->getOption('esi', []);
if (isset($esiOptions['ttl'])) {
$this->setTtl($esiOptions['ttl']);
$e->stopPropagation();
return $this;
}
}
示例14: determineTtl
/**
* @inheritDoc
*/
public function determineTtl(MvcEvent $e)
{
$policy = $this->varnishOptions->getPolicy();
if ($policy === VarnishOptions::POLICY_ALLOW) {
$this->setTtl($this->varnishOptions->getDefaultTtl());
} else {
$this->setTtl(0);
}
$e->stopPropagation();
return $this;
}
示例15: determineTtl
/**
* @inheritDoc
*/
public function determineTtl(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
$routeName = $routeMatch->getMatchedRouteName();
$cacheableRoutes = $this->varnishOptions->getCacheableRoutes();
$ttl = $this->getTtlFor($cacheableRoutes, $routeName);
if (false !== $ttl) {
$this->setTtl($ttl);
$e->stopPropagation();
return $this;
}
}