本文整理汇总了PHP中Zend\EventManager\EventInterface::getResponse方法的典型用法代码示例。如果您正苦于以下问题:PHP EventInterface::getResponse方法的具体用法?PHP EventInterface::getResponse怎么用?PHP EventInterface::getResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\EventManager\EventInterface
的用法示例。
在下文中一共展示了EventInterface::getResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onRoute
public function onRoute(\Zend\EventManager\EventInterface $e)
{
$application = $e->getApplication();
$routeMatch = $e->getRouteMatch();
$sm = $application->getServiceManager();
$auth = $sm->get('Zend\\Authentication\\AuthenticationService');
$config = $sm->get('Config');
$acl = new Acl($config);
// everyone is guest until logging in
$role = Acl::DEFAULT_ROLE;
// The default role is guest $acl
if ($auth->hasIdentity()) {
$user = $auth->getIdentity();
$role = $user->getRole()->getName();
}
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');
if (!$acl->hasResource($controller)) {
throw new \Exception('Resource ' . $controller . ' not defined');
}
if (!$acl->isAllowed($role, $controller, $action)) {
$url = $e->getRouter()->assemble(array(), array('name' => 'home'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
// The HTTP response status code 302 Found is a common way of performing a redirection.
// http://en.wikipedia.org/wiki/HTTP_302
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
}
示例2: onBootstrap
/**
* {@inheritdoc}
*/
public function onBootstrap(EventInterface $e)
{
/** @var \Zend\Mvc\MvcEvent $e */
/** @var \Zend\Mvc\Application $application */
$application = $e->getApplication();
/** @var \Zend\EventManager\EventManager $events */
$events = $application->getEventManager();
/** @var \Zend\EventManager\SharedEventManager $sharedEvents */
$sharedEvents = $events->getSharedManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($events);
// Override Zend\Mvc\View\Http\InjectTemplateListener
// to process templates by Vendor/Module
$injectTemplateListener = new InjectTemplateListener();
$sharedEvents->attach('Zend\\Stdlib\\DispatchableInterface', MvcEvent::EVENT_DISPATCH, [$injectTemplateListener, 'injectTemplate'], -89);
$response = $e->getResponse();
if ($response instanceof \Zend\Http\Response) {
$headers = $response->getHeaders();
if ($headers) {
$headers->addHeaderLine('Cache-Control', 'no-cache, no-store, must-revalidate');
$headers->addHeaderLine('Pragma', 'no-cache');
$headers->addHeaderLine('Expires', '1970-01-01');
}
}
}
示例3: onBootstrap
public function onBootstrap(EventInterface $event)
{
/*$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);*/
$application = $event->getTarget();
$serviceManager = $application->getServiceManager();
$translator = $serviceManager->get('translator');
$translator->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))->setFallbackLocale('en_US');
$application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH, function (MvcEvent $event) use($serviceManager) {
$request = $event->getRequest();
$response = $event->getResponse();
if (!($request instanceof HttpRequest && $response instanceof HttpResponse)) {
return;
// CLI application maybe?
}
$authAdapter = $serviceManager->get('AuthenticationAdapter');
$authAdapter->setRequest($request);
$authAdapter->setResponse($response);
$result = $authAdapter->authenticate();
if ($result->isValid()) {
return;
// OK
}
$response->setContent('Access denied');
$response->setStatusCode(HttpResponse::STATUS_CODE_401);
$event->setResult($response);
// to end
return false;
// event propagation stop
});
}
示例4: onRoute
public function onRoute(\Zend\EventManager\EventInterface $e)
{
$application = $e->getApplication();
$routeMatch = $e->getRouteMatch();
$sm = $application->getServiceManager();
$auth = $sm->get('Zend\\Authentication\\AuthenticationService');
$config = $sm->get('Config');
$acl = new Acl($config);
$role = Acl::DEFAULT_ROLE;
if ($auth->hasIdentity()) {
$user = $auth->getIdentity();
$role = $user->getUserRole()->getRole();
}
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');
if (!$acl->hasResource($controller)) {
throw new \Exception('Resource ' . $controller . ' not defined');
}
if (!$acl->isAllowed($role, $controller, $action)) {
$url = $e->getRouter()->assemble(array(), array('name' => 'home/login'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
}
示例5: onBootstrap
/**
* {@inheritdoc}
*/
public function onBootstrap(EventInterface $e)
{
/** @var \Zend\Mvc\MvcEvent $e */
/** @var \Zend\Mvc\Application $application */
$application = $e->getApplication();
/** @var \Zend\EventManager\EventManager $events */
$events = $application->getEventManager();
/** @var \Zend\EventManager\SharedEventManager $sharedEvents */
$sharedEvents = $events->getSharedManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($events);
// Override Zend\Mvc\View\Http\InjectTemplateListener
// to process templates by Vendor/Module
$injectTemplateListener = new InjectTemplateListener();
$sharedEvents->attach('Zend\\Stdlib\\DispatchableInterface', MvcEvent::EVENT_DISPATCH, [$injectTemplateListener, 'injectTemplate'], -89);
$response = $e->getResponse();
if ($response instanceof \Zend\Http\Response) {
$headers = $response->getHeaders();
if ($headers) {
$headers->addHeaderLine('Cache-Control', 'no-cache, no-store, must-revalidate');
$headers->addHeaderLine('Pragma', 'no-cache');
$headers->addHeaderLine('Expires', '1970-01-01');
$headers->addHeaderLine('X-Frame-Options: SAMEORIGIN');
$headers->addHeaderLine('X-Content-Type-Options: nosniff');
$xssHeaderValue = strpos($_SERVER['HTTP_USER_AGENT'], XssProtection::IE_8_USER_AGENT) === false ? XssProtection::HEADER_ENABLED : XssProtection::HEADER_DISABLED;
$headers->addHeaderLine('X-XSS-Protection: ' . $xssHeaderValue);
}
}
}
示例6: outputCompress
public function outputCompress(EventInterface $e)
{
$app = $e->getApplication();
$eventManager = $app->getEventManager();
$serviceManager = $app->getServiceManager();
$minifyHtmlService = $serviceManager->get('VcoZfMinify\\Service\\MinifyHtmlService');
$response = $e->getResponse();
if (method_exists($response, 'getBody')) {
$response->setContent($minifyHtmlService->minify($response->getBody()));
}
}
示例7: onBootstrap
public function onBootstrap(EventInterface $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$headers = $e->getResponse()->getHeaders();
$headers->addHeaderLine('Access-Control-Allow-Origin: *');
$headers->addHeaderLine('Access-Control-Allow-Methods: PUT, GET, POST, PATCH, DELETE, OPTIONS');
$headers->addHeaderLine('Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept');
$this->initConfig($e);
$e->getApplication()->getEventManager()->attach('render', array($this, 'registerJsonStrategy'), 100);
}
示例8: onRoute
public function onRoute(\Zend\EventManager\EventInterface $e)
{
// Event manager of the app
$application = $e->getApplication();
$routeMatch = $e->getRouteMatch();
$sm = $application->getServiceManager();
$auth = $sm->get('Zend\\Authentication\\AuthenticationService');
$acl = $sm->get('acl');
// everyone is guest until logging in
$role = Acl::DEFAULT_ROLE;
// The default role is guest $acl
if ($auth->hasIdentity()) {
$user = $auth->getIdentity();
$role = $user->getRole()->getName();
}
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');
if (!$acl->hasResource($controller)) {
throw new \Exception('Resource ' . $controller . ' not defined');
}
if (!$acl->isAllowed($role, $controller, $action)) {
$response = $e->getResponse();
$config = $sm->get('config');
$redirect_route = $config['acl']['redirect_route'];
if (!empty($redirect_route)) {
$url = $e->getRouter()->assemble($redirect_route['params'], $redirect_route['options']);
$response->getHeaders()->addHeaderLine('Location', $url);
// The HTTP response status code 302 Found is a common way of performing a redirection.
// http://en.wikipedia.org/wiki/HTTP_302
$response->setStatusCode(302);
$response->sendHeaders();
exit;
} else {
//Status code 403 responses are the result of the web server being configured to deny access,
//for some reason, to the requested resource by the client.
//http://en.wikipedia.org/wiki/HTTP_403
$response->setStatusCode(403);
$response->setContent('
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<h1>403 Forbidden</h1>
</body>
</html>');
return $response;
}
}
}
示例9: onRoute
public function onRoute(\Zend\EventManager\EventInterface $e)
{
$application = $e->getApplication();
$routeMatch = $e->getRouteMatch();
$sm = $application->getServiceManager();
$auth = $sm->get('Zend\\Authentication\\AuthenticationService');
$config = $sm->get('Config');
$acl = new Acl($config);
// everyone is guest untill it gets logged in
$role = Acl::DEFAULT_ROLE;
// The default role is guest $acl
// with Doctrine
if ($auth->hasIdentity()) {
$user = $auth->getIdentity();
$usrlId = $user->getUsrlId();
// Use a view to get the name of the role
// TODO we don't need that if the names of the roles are comming from the DB
switch ($usrlId) {
case 1:
$role = Acl::DEFAULT_ROLE;
// guest
break;
case 2:
$role = 'member';
break;
case 3:
$role = 'admin';
break;
default:
$role = Acl::DEFAULT_ROLE;
// guest
break;
}
}
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');
if (!$acl->hasResource($controller)) {
throw new \Exception('Resource ' . $controller . ' not defined');
}
if (!$acl->isAllowed($role, $controller, $action)) {
$url = $e->getRouter()->assemble(array(), array('name' => 'home'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
// The HTTP response status code 302 Found is a common way of performing a redirection.
// http://en.wikipedia.org/wiki/HTTP_302
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
}
示例10: redirect
protected function redirect(EventInterface $e, $route)
{
$app = $e->getApplication();
$sm = $app->getServiceManager();
/** @var \Zend\Mvc\Router\Http\TreeRouteStack $route */
$currentRoute = $app->getMvcEvent()->getRouteMatch()->getMatchedRouteName();
if ($currentRoute == $route) {
return false;
}
$matchedRoute = $sm->get('Router')->assemble(array(), array('name' => $route));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $matchedRoute);
$response->setStatusCode(302);
$response->sendHeaders();
$e->stopPropagation();
return false;
}
示例11: onRoute
public function onRoute(\Zend\EventManager\EventInterface $e)
{
// Event manager of the app
$application = $e->getApplication();
$routeMatch = $e->getRouteMatch();
$sm = $application->getServiceManager();
$auth = $sm->get('Zend\\Authentication\\AuthenticationService');
$acl = $sm->get('acl');
// everyone is guest until logging in
$role = Acl::DEFAULT_ROLE;
// The default role is guest $acl
if ($auth->hasIdentity()) {
$user = $auth->getIdentity();
$role = $user->getRole()->getName();
}
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');
if (!$acl->hasResource($controller)) {
throw new \Exception('Resource ' . $controller . ' not defined');
}
if (!$acl->isAllowed($role, $controller, $action)) {
$response = $e->getResponse();
$config = $sm->get('config');
$redirect_route = $config['acl']['redirect_route'];
if (!empty($redirect_route['options']['params'])) {
$url = $e->getRouter()->assemble($redirect_route['params'], $redirect_route['options']);
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
exit;
} else {
$response->setStatusCode(403);
$response->setContent('
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<h1>403 Forbidden</h1>
</body>
</html>');
return $response;
}
}
}
示例12: renderLayoutSegments
/**
* Listen to the "render" event and render additional layout segments
*
* @param MvcEvent $e
* @return null
*/
public function renderLayoutSegments(EventInterface $e)
{
// get view Model
$viewModel = $e->getViewModel();
/* @var $viewModel ViewModel */
// add an additional header segment to layout
$header = new ViewModel();
$header->setTemplate('layout/header');
$viewModel->addChild($header, 'header');
// add an additional sidebar segment to layout
$sidebar = new ViewModel();
$sidebar->setTemplate('layout/sidebar');
$viewModel->addChild($sidebar, 'sidebar');
// add an additional footer segment to layout
$footer = new ViewModel();
$footer->setTemplate('layout/footer');
$viewModel->addChild($footer, 'footer');
// return response
return $e->getResponse();
}
示例13: renderSeo
public function renderSeo(EventInterface $e)
{
$sm = $e->getApplication()->getServiceManager();
$config = $sm->get('config');
$routes = $config['seo']['seo_routes'];
$seoRoute = $e->getRouteMatch()->getMatchedRouteName();
$params = $e->getRouteMatch()->getParams();
$id = $params['id'];
$type = $routes[$seoRoute];
$seo = $this->seoService->getSeoByTypeId($id, $type);
if ($seo) {
// get view Model
$renderer = $sm->get('Zend\\View\\Renderer\\PhpRenderer');
$renderer->headTitle()->append(ucfirst($seo->getTitle()));
$renderer->headMeta()->appendName('description', $seo->getDescription());
$renderer->headMeta()->setName('keywords', $seo->getKeywords());
}
// return response
return $e->getResponse();
}
示例14: onBootstrap
public function onBootstrap(EventInterface $e)
{
/**
* Log any Uncaught Exceptions, including all Exceptions in the stack
*/
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
$sm = $e->getApplication()->getServiceManager();
$config = $sm->get('Config');
if (isset($config['VcoZfLogger']) && isset($config['VcoZfLogger']['exceptionhandler']) && $config['VcoZfLogger']['exceptionhandler'] === true) {
$sharedManager->attach('Zend\\Mvc\\Application', 'dispatch.error', function ($e) use($sm) {
$response = $e->getResponse();
$ex = $e->getParam('exception');
$logger = $sm->get('VcoZfLogger');
$statusCode = method_exists($response, 'getStatusCode') ? $response->getStatusCode() : null;
$priority = $statusCode == 404 ? Logger::ERR : Logger::CRIT;
if ($ex) {
do {
$logger->log($priority, sprintf("%s:%d %s (%d) [%s]", $ex->getFile(), $ex->getLine(), $ex->getMessage(), $ex->getCode(), get_class($ex)));
} while ($ex = $ex->getPrevious());
}
});
}
}
示例15: onRoute
public function onRoute(EventInterface $e)
{
$application = $e->getApplication();
$routeMatch = $e->getRouteMatch();
$sm = $application->getServiceManager();
// Authentication
// $auth = $sm->get('Zend\Authentication\AuthenticationService');
/**
* @Todo check if session container 'User' still exists
*/
$UserContainer = new Container('User');
//Authorization with database (check module.config.php)
$acl = $sm->get('acl');
// everyone is guest until it gets logged in
$role = AclDb::DEFAULT_ROLE;
if ($UserContainer->id) {
$role = $UserContainer->activeRole;
}
$resource = $routeMatch->getParam('controller');
$privilege = $routeMatch->getParam('action');
if (!$acl->hasResource($resource)) {
throw new \Exception('Resource ' . $resource . ' not defined');
}
if (!$acl->isAllowed($role, $resource, $privilege)) {
// Get acl configuration to redirect route
$response = $e->getResponse();
$config = $sm->get('config');
$redirect_route = $config['acl']['redirect_route'];
$url = $e->getRouter()->assemble($redirect_route['params'], $redirect_route['options']);
$response->getHeaders()->addHeaderLine('Location', $url);
// The HTTP response status code 302 Found is a common way of performing a redirection.
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
}