本文整理汇总了PHP中Zend\Mvc\MvcEvent::getTarget方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getTarget方法的具体用法?PHP MvcEvent::getTarget怎么用?PHP MvcEvent::getTarget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getTarget方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: layoutChange
public function layoutChange(MvcEvent $event)
{
// Change By Route Defaults
$routeMatch = $event->getRouteMatch();
if ($routeMatch && ($layout = $routeMatch->getParam('layout', false))) {
$event->getTarget()->layout($layout);
return;
}
/** @var \Zend\Http\PhpEnvironment\Request $request */
$request = $event->getRequest();
$uri = $request->getServer('REQUEST_URI');
$config = $this->getConfig($event->getParam('application')->getConfig());
// Change Layout by URI
foreach ($config['uri'] as $var => $value) {
$position = strpos($var, '*');
if ($position !== false) {
$uri = substr($uri, 0, $position);
$var = substr($var, 0, $position);
}
if ($var == $uri) {
if (method_exists($target = $event->getTarget(), 'layout')) {
$target->layout($value);
} else {
$event->getViewModel()->setTemplate($value);
}
}
}
}
示例3: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$eventManager = $app->getEventManager();
#$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');
if (isset($config['module_layouts'][$moduleNamespace])) {
$controller->layout($config['module_layouts'][$moduleNamespace]);
}
}, 100);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$shared = $eventManager->getSharedManager();
$sm = $app->getServiceManager();
$shared->attach('ZfcUser\\Service\\User', 'register.post', function ($e) use($sm) {
$userService = $e->getTarget();
$sm = $userService->getServiceManager();
$em = $sm->get('doctrine.entitymanager.orm_default');
$newUser = $e->getParam('user');
#$registrationForm = $e->getParam('form');
$config = $sm->get('config');
$criteria = array('roleId' => $config['bjyauthorize']['new_user_default_role']);
$defaultUserRole = $em->getRepository('ErsBase\\Entity\\UserRole')->findOneBy($criteria);
if ($defaultUserRole !== null) {
$newUser->addRole($defaultUserRole);
$em->persist($newUser);
$em->flush();
}
});
}
示例4: afterDispatch
public function afterDispatch(MvcEvent $e)
{
$controllerName = $e->getRouteMatch()->getMatchedRouteName();
if ($controllerName != 'login' && ($controllerName != 'application' && $controllerName != 'home')) {
$containerSession = new \Zend\Session\Container('cbol');
$e->getTarget()->layout()->repo = $containerSession->reportesVias;
$e->getTarget()->layout()->acceso = $containerSession->permisosUser;
$e->getTarget()->layout()->suge = $containerSession->sugerencias;
$auth = new \Zend\Authentication\AuthenticationService();
$response = $e->getResponse();
if (!$auth->hasIdentity()) {
$url = $e->getRequest()->getBaseUrl() . '/login';
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
return $response;
} else {
$localAcl = new \Login\Model\permisos();
if (!$localAcl->isAllowed($auth->getIdentity()->perfil_id, $controllerName)) {
$this->onDispatchError($e, $controllerName);
} elseif (is_null($containerSession->idSession)) {
$url = $e->getRequest()->getBaseUrl() . '/login/logout';
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
return $response;
} elseif ($e->getResponse()->getStatusCode() == 403) {
$this->onDispatchError($e, $controllerName);
}
}
}
}
示例5: handleControllerCannotDispatchRequest
public function handleControllerCannotDispatchRequest(MvcEvent $e)
{
$action = $e->getRouteMatch()->getParam('action');
$controller = get_class($e->getTarget());
// error-controller-cannot-dispatch
if (!method_exists($e->getTarget(), $action . 'Action')) {
$logText = 'The requested controller ' . $controller . ' was unable to dispatch the request : ' . $action . 'Action';
//you can do logging, redirect, etc here..
echo $logText;
}
}
示例6: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$serviceLocator = $e->getApplication()->getServiceManager();
$e->getApplication()->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, function (MvcEvent $e) use($serviceLocator) {
if (!$e->getRequest() instanceof Request) {
return;
}
$request = $e->getRequest();
$config = $serviceLocator->get('Config');
$serviceName = isset($config['hermes']['service_name']) ? $config['hermes']['service_name'] : '';
$data = ['direction' => 'in', 'status' => 'success', 'destination' => ['service' => $serviceName, 'server' => $request->getUri()->getHost(), 'uri' => $request->getUriString()], 'source' => ['server' => $_SERVER['REMOTE_ADDR'], 'service' => $request->getHeader('X-Request-Name') ? $request->getHeader('X-Request-Name')->getFieldValue() : '', 'uri' => '']];
if (!$e->getRequest()->isGet()) {
$post = json_decode($e->getRequest()->getContent(), true, 100);
unset($post['password']);
$data['data'] = $post;
}
\LosLog\Log\RequestLogger::save($e->getRequest(), $data);
}, 100);
$hermes = $serviceLocator->get('hermes');
$em = $hermes->getEventManager();
$em->attach('request.post', function (Event $e) use($serviceLocator) {
/* @var \Hermes\Api\Client $hermes */
$hermes = $e->getTarget();
$request = $hermes->getZendClient()->getRequest();
$config = $serviceLocator->get('Config');
$serviceName = isset($config['hermes']['service_name']) ? $config['hermes']['service_name'] : '';
$data = ['direction' => 'out', 'status' => 'success', 'source' => ['service' => $serviceName, 'server' => $_SERVER['SERVER_ADDR'], 'uri' => $_SERVER['REQUEST_URI']], 'destination' => ['service' => $hermes->getServiceName(), 'server' => $request->getUri()->getHost(), 'uri' => $request->getUriString()], 'http_code' => $hermes->getZendClient()->getResponse()->getStatusCode()];
if (!$request->isGet()) {
$post = json_decode($request->getContent(), true, 100);
unset($post['password']);
$data['data'] = $post;
}
\LosLog\Log\RequestLogger::save($request, $data);
}, 100);
$em->attach('request.fail', function (Event $e) use($serviceLocator) {
/* @var \Hermes\Api\Client $hermes */
$hermes = $e->getTarget();
$request = $hermes->getZendClient()->getRequest();
$config = $serviceLocator->get('Config');
$serviceName = isset($config['hermes']['service_name']) ? $config['hermes']['service_name'] : '';
$data = ['direction' => 'out', 'status' => 'failed', 'source' => ['service' => $serviceName, 'server' => $_SERVER['SERVER_ADDR'], 'uri' => $_SERVER['REQUEST_URI']], 'destination' => ['service' => $hermes->getServiceName(), 'server' => $request->getUri()->getHost(), 'uri' => $request->getUriString()]];
if (!$request->isGet()) {
$post = json_decode($request->getContent(), true, 100);
unset($post['password']);
$data['data'] = $post;
}
$exception = $e->getParams();
$data['http_code'] = $exception->getCode();
$data['error'] = $exception->getMessage();
\LosLog\Log\RequestLogger::save($request, $data);
}, 100);
}
示例7: 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());
}
}
示例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: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$application = $e->getApplication();
$sm = $application->getServiceManager();
$sharedManager = $eventManager->getSharedManager();
//Setting layouts
$sharedManager->attach('Zend\\Mvc\\Controller\\AbstractController', 'dispatch', function ($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$config = $e->getApplication()->getServiceManager()->get('config');
if (isset($config['module_layouts'][$moduleNamespace])) {
$controller->layout($config['module_layouts'][$moduleNamespace]);
}
}, 100);
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
if (null !== $matchedRoute) {
//Check the Authentication in every controller different with Login
//If there is no identity this will redirect to Login
$sharedManager->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function () use($sm) {
$sm->get('ControllerPluginManager')->get('Authentication')->isAuthtenticated();
}, 2);
// @todo implement ACL
//Check ACL and show the menu
//$sharedManager->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function() use ($sm) {
// $sm->get('ControllerPluginManager')->get('Privileges')->doAuthorization();
//}, 2);
}
}
示例10: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) {
$auth = $e->getApplication()->getServiceManager()->get("Auth_Service");
$application = $e->getApplication();
$viewModel = $application->getMvcEvent()->getViewModel();
$viewModel->UserAuth = false;
if ($auth->hasIdentity()) {
$viewModel->UserAuth = true;
$viewModel->UserName = $auth->getStorage()->read()->name;
}
$routeMatch = $e->getRouteMatch();
$controller = $routeMatch->getParam('controller');
$whitelist = array('Customers\\Controller\\Login');
if (!in_array($controller, $whitelist)) {
if (!$auth->hasIdentity()) {
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function ($e) {
$controller = $e->getTarget();
$controller->plugin('redirect')->toRoute('customers');
}, 100);
}
}
});
// $config = new StandardConfig();
// $config->setOptions(array(
// 'remember_me_seconds' => 10,
// 'name' => 'zf2',
// ));
// $manager = new SessionManager($config);
}
示例11: addRoutes
/**
* addRoutes Event Handler
* set static pages routes
*
* @access public
* @param MvcEvent $event
*/
public function addRoutes(MvcEvent $event)
{
$serviceManager = $event->getTarget()->getServiceManager();
$routeEvent = $serviceManager->get('CMS\\Event\\RouteEvent');
$routeConfig = $routeEvent->addStaticPagesRoutes($event);
$this->addedRoutes = $routeConfig;
}
示例12: onBootstrap
/**
* Override default rendering strategy
*
* @param MvcEvent $e
*/
public function onBootstrap(MvcEvent $e)
{
// attach the JSON view strategy
$app = $e->getTarget();
/** @var ServiceLocator $locator */
$locator = $app->getServiceManager();
/**
* @var View $view
*/
$view = $locator->get('ZendViewView');
/**
* @var ViewJsonStrategyFactory $strategy
*/
$strategy = $locator->get('ViewJsonStrategy');
$view->getEventManager()->attach($strategy, 100);
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
//Add listener to check the response
// Allow possibility to return a simple array
$eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
//Add listener for rendering the error
$eventManager->attach(MvcEvent::EVENT_RENDER, array($this, 'onRenderError'));
//Add listener to finish the execution of request
$eventManager->attach(MvcEvent::EVENT_FINISH, array($this, 'onFinish'));
}
示例13: 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);
}
示例14: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
//$eventManager->attach(MvcEvent::EVENT_DISPATCH,array($this,'authPreDispatch'),1);
/**
* This allows to set the layout by module by action. By default
* it gets layout\layout. This gets the modules from module_layouts
* array in module.config.php.
*/
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function ($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$controllerName = explode('\\', $controllerClass);
$controllerName = str_replace('Controller', '', array_pop($controllerName));
$config = $e->getApplication()->getServiceManager()->get('config');
$routeMatch = $e->getRouteMatch();
$actionName = strtolower($routeMatch->getParam('action', 'not-found'));
// get the action name
if (isset($config['module_layouts'][$moduleNamespace][$controllerName][$actionName])) {
$controller->layout($config['module_layouts'][$moduleNamespace][$controllerName][$actionName]);
} elseif (isset($config['module_layouts'][$moduleNamespace][$controllerName]['default'])) {
$controller->layout($config['module_layouts'][$moduleNamespace][$controllerName]['default']);
}
}, -100);
}
示例15: loadConfiguration
public function loadConfiguration(MvcEvent $e)
{
$controller = $e->getTarget();
$user_session = new Container('usuario');
$controller->layout()->nome = trim(substr($user_session->nome, 0, 16));
$controller->layout()->foto = $user_session->foto ? "/fotos/" . str_replace(" ", "_", $user_session->foto) : "/fotos/default.jpg";
}