本文整理汇总了PHP中Zend\Mvc\MvcEvent::getApplication方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getApplication方法的具体用法?PHP MvcEvent::getApplication怎么用?PHP MvcEvent::getApplication使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getApplication方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$serviceManager = $e->getApplication()->getServiceManager();
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleException'));
//set the global exception handler
//set_exception_handler( array( $this, 'handleException' ) );
// Disable layout for ajax requests
// $eventManager->attach(MvcEvent::EVENT_DISPATCH, function ($e)
// {
// IF its an AJAX request & its not in JSON Model form yet
// then it has been JMS serialized & thus just needs to be printed
// if ($e->getRequest()->isXmlHttpRequest() && ! ($result instanceof \Zend\View\Model\JsonModel) ) {
//$viewModel = new \Zend\View\Model\ViewModel(array(
// 'content' => $result,
//));
//$viewModel->setTemplate('layout/empty');
//$e->setViewModel($viewModel);
// }
// });
// Ensuring services are available to be called in the application model
// by setting the registry into a static variable
// This enforces usage of service locator pattern instead of dependency injection
\Application\Model\CommonServiceMutatorTrait::setServiceLocator($serviceManager);
}
示例2: getEntityManager
/**
* @return \Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
if (null === $this->entityManager) {
$this->entityManager = $this->event->getApplication()->getServiceManager()->get('Doctrine\\ORM\\EntityManager');
}
return $this->entityManager;
}
示例3: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$events = $e->getApplication()->getEventManager()->getSharedManager();
$config = $e->getApplication()->getServiceManager()->get('config');
// configure session
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config['session']);
$events->attach('ZfcUser\\Service\\User', 'register.post', function ($e) {
$user = $e->getParam('user');
// User account object
$form = $e->getParam('form');
// Form object
// Perform your custom action here
/* @var $sm ServiceLocatorInterface */
$sm = $e->getTarget()->getServiceManager();
/* @var $em \Doctrine\ORM\EntityManager */
$em = $sm->get('doctrine.entitymanager.orm_default');
$userRole = $em->find(__NAMESPACE__ . '\\Entity\\UserRole', DEFAULT_ROLE);
if (NULL !== $userRole) {
$user->addRole($userRole);
$em->persist($user);
$em->flush();
}
});
}
示例4: dispatchError
public function dispatchError(MvcEvent $e)
{
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
$sharedManager->attach('Zend\\Mvc\\Application', 'dispatch.error', function ($e) {
if ($e->getParam('exception')) {
ob_clean();
//Limpar a tela de erros do php
header('HTTP/1.1 400 Bad Request');
$exception = $e->getParam('exception');
$sm = $e->getApplication()->getServiceManager();
$config = $sm->get('Config');
$e->getApplication()->getServiceManager()->get('Controller\\Plugin\\Manager')->get('jsLog')->log($exception, 2);
$viewModel = new \Zend\View\Model\ViewModel(['exception' => $exception]);
if ($e->getRequest()->isXmlHttpRequest()) {
$viewModel->setTemplate($config['js_library']['error_ajax_exception']);
$e->getApplication()->getServiceManager()->get('ViewRenderer')->render($viewModel);
} else {
$viewModel->setTemplate($config['js_library']['error_exception']);
echo $e->getApplication()->getServiceManager()->get('ViewRenderer')->render($viewModel);
}
/*
* Com erros handler o codigo continua a ser executado,
* entao o exit para e so mostra os erros
*/
exit;
}
});
}
示例5: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
// Add translation
$translator = $e->getApplication()->getServiceManager()->get('translator');
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$setLang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
} else {
$setLang = '';
}
$translator->setLocale(\Locale::acceptFromHttp($setLang))->setFallbackLocale('en_US');
// Add ACL information to the Navigation view helper
$sm = $e->getApplication()->getServiceManager();
$authorize = $sm->get('BjyAuthorize\\Service\\Authorize');
$acl = $authorize->getAcl();
$role = $authorize->getIdentity();
\Zend\View\Helper\Navigation::setDefaultAcl($acl);
\Zend\View\Helper\Navigation::setDefaultRole($role);
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\\Mvc\\Application', 'dispatch.error', function ($e) use($sm) {
if ($e->getParam('exception')) {
$sm->get('Zend\\Log\\Logger')->crit($e->getParam('exception'));
}
});
}
示例6: onBootstrap
public function onBootstrap(MvcEvent $e)
{
/*
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initSession(array(
'remember_me_seconds' => 180,
'use_cookies' => true,
'cookie_httponly' => true,
));
*/
$eventManager = $e->getApplication()->getEventManager();
$eventManager->getSharedManager()->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function ($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$config = $e->getApplication()->getServiceManager()->get('config');
//echo '<pre>'; print_r($config);exit;
if (isset($config['module_layouts'][$moduleNamespace])) {
$controller->layout($config['module_layouts'][$moduleNamespace]);
}
}, 100);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initSession(array('remember_me_seconds' => 180, 'use_cookies' => true, 'cookie_httponly' => true));
}
示例7: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$callback = function (MvcEvent $event) {
$view = $event->getApplication()->getServiceManager()->get('ViewRenderer');
$config = $event->getApplication()->getConfig();
$controller = $event->getTarget();
$rm = $event->getRouteMatch();
if (!$rm instanceof RouteMatch) {
$rm = new RouteMatch(array('module' => 'Application', '__NAMESPACE__' => 'Application\\Controller', '__CONTROLLER__' => 'index', 'controller' => 'Application\\Controller\\Index', 'action' => 'index'));
}
$params = $rm->getParams();
$modulo = "";
if (isset($params['__NAMESPACE__'])) {
$paramsArray = explode("\\", $params['__NAMESPACE__']);
$modulo = $paramsArray[0];
}
$controller = isset($params['__CONTROLLER__']) ? $params['__CONTROLLER__'] : "";
$action = isset($params['action']) ? $params['action'] : null;
$app = $event->getParam('application');
$sm = $app->getServiceManager();
$paramsConfig = ['modulo' => strtolower($modulo), 'controller' => strtolower($controller), 'action' => strtolower($action), 'baseHost' => $view->base_path("/"), 'cssStaticHost' => "", 'jsStaticHost' => "", 'statHost' => "", 'eHost' => "", 'statVers' => '?', 'min' => '', 'AppCore' => [], 'AppSandbox' => [], 'AppSchema' => ['modules' => [], 'requires' => []]];
$view->inlineScript()->appendScript("var yOSON=" . json_encode($paramsConfig, JSON_FORCE_OBJECT));
};
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\\Mvc\\Controller\\AbstractActionController', MvcEvent::EVENT_DISPATCH, $callback, 100);
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\\Mvc\\Application', MvcEvent::EVENT_DISPATCH_ERROR, $callback, 100);
}
示例8: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$routeHandler = $sm->get('MultiRoleAclBase\\Handler\\RouteHandler');
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach(MvcEvent::EVENT_ROUTE, array($routeHandler, 'handler'), -100);
}
示例9: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
$serviceManager = $application->getServiceManager();
$eventManager = $application->getEventManager();
$sharedManager = $eventManager->getSharedManager();
// DISPATCH EVENT
$sharedManager->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function ($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
if ($moduleNamespace == 'Frontend') {
// frontend layout
// wenn der benutzer eingeloggt ist darf er das admin panel betreten?
$authService = $e->getApplication()->getServiceManager()->get('AuthService');
// normal layout
$controller->layout('layout/frontend');
/**
if( $authService->isLoggedIn() ) {
// layout für registrierte user
$controller->layout('layout/frontend/registered');
} else {
// normal layout unregistriert
$controller->layout('layout/frontend');
}
**/
}
}, 50);
}
示例10: onBootstrap
/**
* @param MvcEvent $event
*/
public function onBootstrap(MvcEvent $event)
{
$eventManager = $event->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$application = $event->getApplication();
$eventManager = $application->getEventManager();
/* @var $serviceManager ServiceManager */
$serviceManager = $application->getServiceManager();
$config = $serviceManager->get('config');
if (array_key_exists('locale', $config)) {
$config = $config['locale'];
} else {
$config = null;
}
if (!$config) {
Locale::setDefault('en');
// hardcoded here
$translator = $serviceManager->get('translator');
AbstractValidator::setDefaultTranslator($translator);
return;
}
$settings = new Settings($config);
// set the locale based on URL from route or cookie & set the default translator to all the app
$eventManager->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $e) use($config, $settings, $serviceManager) {
$settings->setRouteMatch($e->getRouteMatch());
if (!$e->getRequest() instanceof Request) {
$settings->setRequest($e->getRequest());
}
\Locale::setDefault($settings->getLocale());
$translator = $serviceManager->get('translator');
AbstractValidator::setDefaultTranslator($translator);
}, -9);
}
示例11: onBootstrap
/**
* Attach authentication event listeners
*
* @param MvcEvent $event
*/
public function onBootstrap(MvcEvent $event)
{
$shared = $event->getApplication()->getEventManager()->getSharedManager();
$services = $event->getApplication()->getServiceManager();
$listener = $services->get(ApiKeyListener::class);
$shared->attach('Zend\\Mvc\\Application', MvcAuthEvent::EVENT_AUTHENTICATION, [$listener, 'authenticate'], 1000);
}
示例12: onBootstrap
public function onBootstrap(MvcEvent $e)
{
if (\Zend\Console\Console::isConsole()) {
return;
}
$eventManager = $e->getApplication()->getEventManager();
$services = $e->getApplication()->getServiceManager();
// TODO: Löschen sobald die Lösung mit der LoginBox klappt
//$eventManager->attach(
// array(MvcEvent::EVENT_RENDER, MvcEvent::EVENT_RENDER_ERROR),
// array(new InjectLoginInfoListener(), 'injectLoginInfo'), -1000
//);
$eventManager->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $e) use($services) {
/** @var CheckPermissionsListener $checkPermissionsListener */
$checkPermissionsListener = $services->get('Auth/CheckPermissionsListener');
$checkPermissionsListener->onRoute($e);
}, -10);
$eventManager->attach(MvcEvent::EVENT_DISPATCH, function (MvcEvent $e) use($services) {
/** @var CheckPermissionsListener $checkPermissionsListener */
$checkPermissionsListener = $services->get('Auth/CheckPermissionsListener');
$checkPermissionsListener->onDispatch($e);
}, 10);
$unauthorizedAccessListener = $services->get('UnauthorizedAccessListener');
$unauthorizedAccessListener->attach($eventManager);
$sharedManager = $eventManager->getSharedManager();
$defaultlistener = $services->get('Auth/Listener/AuthAggregateListener');
$defaultlistener->attachShared($sharedManager);
}
示例13: onBootstrap
public function onBootstrap(MvcEvent $e)
{
/* @var $sm \Zend\ServiceManager\ServiceManager */
$sm = $e->getApplication()->getServiceManager();
$userEntityClass = $sm->get('zfcuser_module_options')->getUserEntityClass();
$classRef = new \ReflectionClass($userEntityClass);
if (!$classRef->implementsInterface('Zf2SimpleAcl\\Entity\\UserInterface')) {
throw new \InvalidArgumentException($userEntityClass . ' must implement Zf2SimpleAcl\\Entity\\UserInterface');
}
$eventManager = $e->getApplication()->getEventManager();
$recognizers = $sm->get('zf2simpleacl_module_options')->getRecognizers();
$authService = new AuthenticationService($sm->get('zfcuserauthservice'));
if (!empty($recognizers) && count($recognizers)) {
$authService = new AuthenticationAggregator();
$authService->addService(new AuthenticationService($sm->get('zfcuserauthservice')));
foreach ($recognizers as $recognizer) {
if (!$sm->has($recognizer)) {
if (!$sm->has('zf2simpleacl_recognizer_' . $recognizer)) {
throw new RuntimeException("Could not find recognizer service with name\n [{$recognizer}] nor with [zf2simpleacl_recognizer_{$recognizer}]");
} else {
$recognizer = 'zf2simpleacl_recognizer_' . $recognizer;
}
}
$authService->addService($sm->get($recognizer));
}
}
$eventManager->attach(new RouteGuard($sm->get('zf2simpleacl_acl'), $authService));
$eventManager->attach(new RedirectionStrategy($sm->get('zf2simpleacl_redirect_route')));
$eventManager->attach(new ForbiddenStrategy());
}
示例14: errorProcess
/**
* @param MvcEvent $e
* @return null|\Zend\Http\PhpEnvironment\Response
*/
public function errorProcess(MvcEvent $e)
{
/** @var \Zend\Di\Di $di */
$di = $e->getApplication()->getServiceManager()->get('di');
$eventParams = $e->getParams();
/** @var array $configuration */
$configuration = $e->getApplication()->getConfig();
$vars = array();
if (isset($eventParams['exception'])) {
/** @var \Exception $exception */
$exception = $eventParams['exception'];
if ($configuration['errors']['show_exceptions']['message']) {
$vars['error-message'] = $exception->getMessage();
}
if ($configuration['errors']['show_exceptions']['trace']) {
$vars['error-trace'] = $exception->getTrace();
}
}
if (empty($vars)) {
$vars['error'] = 'Something went wrong';
}
/** @var PostProcessor\AbstractPostProcessor $postProcessor */
$postProcessor = $di->get($configuration['errors']['post_processor'], array('vars' => $vars, 'response' => $e->getResponse()));
$postProcessor->process();
if ($eventParams['error'] === \Zend\Mvc\Application::ERROR_CONTROLLER_NOT_FOUND || $eventParams['error'] === \Zend\Mvc\Application::ERROR_ROUTER_NO_MATCH) {
$e->getResponse()->setStatusCode(\Zend\Http\PhpEnvironment\Response::STATUS_CODE_501);
} else {
$e->getResponse()->setStatusCode(\Zend\Http\PhpEnvironment\Response::STATUS_CODE_500);
}
$e->stopPropagation();
return $postProcessor->getResponse();
}
示例15: onBootstrap
public function onBootstrap(\Zend\Mvc\MvcEvent $e)
{
$evm = $e->getApplication()->getEventManager();
$evm->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, function (\Zend\Mvc\MvcEvent $e) {
$evm = $e->getApplication()->getEventManager();
$rm = $e->getRouteMatch();
if ($rm->getParam('cronLock')) {
//Work out the filename of the lock file
$config = $e->getApplication()->getServiceManager()->get('Config');
$dir = isset($config['cronLockDir']) ? $config['cronLockDir'] : '/tmp';
$filename = preg_replace("([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\).])", '', $rm->getMatchedRouteName());
$file = "{$dir}/{$filename}";
$this->fp = fopen($file, "w");
if (flock($this->fp, LOCK_EX | LOCK_NB)) {
//The cron action is not locked
$evm->attach(MvcEvent::EVENT_DISPATCH, function ($e) {
flock($this->fp, LOCK_UN);
});
} else {
//This cron action is locked, so redirect and terminate
$rm->setMatchedRouteName('lock-cron')->setParam('controller', 'CronLock\\Controller\\CronLock')->setParam('action', 'lockCron');
$e->setRouteMatch($rm);
}
}
return $e;
});
}