本文整理汇总了PHP中Zend\Mvc\MvcEvent::getViewModel方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getViewModel方法的具体用法?PHP MvcEvent::getViewModel怎么用?PHP MvcEvent::getViewModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getViewModel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: initAcl
/**
* Add the ACL of this module to the global ACL
*
* @param MvcEvent $e
* @return void
*/
public function initAcl(MvcEvent $e)
{
if ($e->getViewModel()->acl == null) {
$acl = new Acl();
} else {
$acl = $e->getViewModel()->acl;
}
$aclConfig = (include __DIR__ . '/config/module.acl.php');
$allResources = array();
foreach ($aclConfig['roles'] as $role) {
if (!$acl->hasRole($role)) {
$role = new Role($role);
$acl->addRole($role);
} else {
$role = $acl->getRole($role);
}
if (array_key_exists($role->getRoleId(), $aclConfig['permissions'])) {
foreach ($aclConfig['permissions'][$role->getRoleId()] as $resource) {
if (!$acl->hasResource($resource)) {
$acl->addResource(new Resource($resource));
}
$acl->allow($role, $resource);
}
}
}
$e->getViewModel()->acl = $acl;
}
示例2: 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);
}
示例3: injectLayoutMenu
public function injectLayoutMenu(MvcEvent $event)
{
$viewModel = $event->getViewModel();
if ($viewModel instanceof ViewModel && $viewModel->getTemplate() == 'layout/layout') {
$access = $event->getApplication()->getServiceManager()->get(ServiceFactory::SERVICE);
if ($access->isAuthorized()) {
$viewModel = new ViewModel();
$viewModel->setTemplate('spork-tools/footer');
$event->getViewModel()->addChild($viewModel, 'sporkToolsMenu');
}
}
}
示例4: dispatch
public function dispatch(MvcEvent $event)
{
$auth = new AuthenticationService();
if (!$auth->hasIdentity()) {
$router = $event->getRouteMatch();
if (method_exists($router, 'getMatchedRouteName') && $router->getMatchedRouteName() == 'home') {
$event->getViewModel()->setTemplate('layout/landing');
} else {
$event->getViewModel()->setTemplate('layout/anonymous');
}
}
}
示例5: checkAcl
public function checkAcl(MvcEvent $e)
{
$route = $e->getRouteMatch()->getMatchedRouteName();
//you set your role
$userRole = 'guest';
if ($e->getViewModel()->acl->hasResource($route) && !$e->getViewModel()->acl->isAllowed($userRole, $route)) {
$response = $e->getResponse();
//location to page or what ever
$response->getHeaders()->addHeaderLine('Location', $e->getRequest()->getBaseUrl() . '/404');
$response->setStatusCode(404);
}
}
示例6: updateLayout
/**
* Switch layout based on user's role
*
* @param MvcEvent $e
*/
public function updateLayout(MvcEvent $e)
{
$params = $e->getRouteMatch()->getParams();
if (isset($params['backend']) && $params['backend'] && Authentication::getInstance()->hasIdentity()) {
$config = $e->getApplication()->getServiceManager()->get('config');
$role = Authentication::getInstance()->getIdentity()->role;
$layouts = $config['acl']['backend_layout'];
if ($role && isset($layouts['roles'][$role])) {
$e->getViewModel()->setTemplate($layouts['roles'][$role]);
} else {
$e->getViewModel()->setTemplate($layouts['default']);
}
}
}
示例7: collect
/**
* @inheritdoc
*/
public function collect(MvcEvent $mvcEvent)
{
$templates = array();
$match = $mvcEvent->getRouteMatch();
$templates[] = $mvcEvent->getViewModel()->getTemplate();
if ($mvcEvent->getViewModel()->hasChildren()) {
foreach ($mvcEvent->getViewModel()->getChildren() as $child) {
$templates[] = $child->getTemplate();
}
}
if (empty($templates)) {
$templates[] = 'N/A';
}
$this->data = array('templates' => $templates, 'method' => $mvcEvent->getRequest()->getMethod(), 'status' => $mvcEvent->getResponse()->getStatusCode(), 'route' => $match === null ? 'N/A' : $match->getMatchedRouteName(), 'action' => $match === null ? 'N/A' : $match->getParam('action', 'N/A'), 'controller' => $match === null ? 'N/A' : $match->getParam('controller', 'N/A'));
}
示例8: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getViewModel()->tags = $e->getApplication()->getServiceManager()->get('TagService')->getMostPopularTags();
}
示例9: __invoke
public function __invoke(MvcEvent $event)
{
if ($event->getViewModel()->terminate()) {
/*
* No need for navigation when it is not rendered anyway.
*/
return;
}
$services = $event->getApplication()->getServiceManager();
$navigation = $services->get('Core/Navigation');
$settingsMenu = $navigation->findOneBy('route', 'lang/settings');
if ($settingsMenu->hasChildren()) {
/*
* We already have the subnavigation.
*/
return;
}
$moduleManager = $services->get('ModuleManager');
$configPlugin = $services->get('ControllerPluginManager')->get('config');
$modules = $moduleManager->getLoadedModules();
$modulesWithSettings = $configPlugin("settings", array_keys($modules));
$routeMatch = $event->getRouteMatch();
$router = $event->getRouter();
$activeModule = $event->getParam('__settings_active_module', false);
$settingsMenu->setActive((bool) $activeModule);
foreach ($modulesWithSettings as $key => $param) {
$page = array('label' => isset($param['navigation_label']) ? $param['navigation_label'] : ucfirst($key), 'order' => isset($param['navigation_order']) ? $param['navigation_order'] : '10', 'class' => isset($param['navigation_class']) ? $param['navigation_class'] : null, 'resource' => 'route/lang/settings', 'route' => 'lang/settings', 'router' => $router, 'action' => 'index', 'controller' => 'index', 'params' => array('lang' => 'de', 'module' => $key), 'active' => $key == $activeModule);
if ($routeMatch instanceof RouteMatch) {
$page['routeMatch'] = $routeMatch;
}
$settingsMenu->addPage($page);
}
}
示例10: checkAuth
public function checkAuth(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$auth = $sm->get('AuthService');
$match = $e->getRouteMatch();
if (!$match instanceof RouteMatch) {
return;
}
$name = $match->getMatchedRouteName();
if (in_array($name, $this->publicRoutes)) {
return;
}
//Check identity
if ($auth->hasIdentity()) {
$viewModel = $e->getViewModel();
$viewModel->current_user = $auth->getIdentity();
return;
}
$router = $e->getRouter();
$url = $router->assemble(array(), array('name' => 'auth'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
示例11: 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;
}
示例12: injectIdentityModel
/**
* Inject identity view model into layout
*
* @param MvcEvent $event
* @throws \Exception
*/
public function injectIdentityModel(MvcEvent $event)
{
$viewModel = $event->getViewModel();
if ($viewModel->getTemplate() == 'layout/layout') {
$servies = $event->getApplication()->getServiceManager();
$appConfig = $servies->get('config');
if (isset($appConfig['view_model_identity'])) {
$config = $appConfig['view_model_identity'];
} else {
throw new \Exception('view_model_identity key not found in configuration');
}
if (!$servies->has($config['authenticationService'])) {
throw new \Exception('Auththentication service not found');
}
if (!$servies->has($config['identity'])) {
throw new \Exception('Identity not found');
}
$childViewModel = new IdentityViewModel(array('auth' => $servies->get($config['authenticationService']), 'identity' => $servies->get($config['identity'])));
if (isset($config['template'])) {
$childViewModel->setTemplate($config['template']);
}
if (isset($config['captureTo'])) {
$childViewModel->setCaptureTo($config['captureTo']);
}
$viewModel->addChild($childViewModel);
}
}
示例13: onDispatch
public function onDispatch(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$categories = $sm->get('categories');
$vm = $e->getViewModel();
$vm->setVariable('categories', $categories);
}
示例14: 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);
}
}
}
}
示例15: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
// Show flashmessages in the view
$eventManager->attach(MvcEvent::EVENT_RENDER, function ($e) {
$flashMessenger = new FlashMessenger();
$messages = array();
$flashMessenger->setNamespace('success');
if ($flashMessenger->hasMessages()) {
$messages['success'] = $flashMessenger->getMessages();
}
$flashMessenger->clearMessages();
$flashMessenger->setNamespace('warning');
if ($flashMessenger->hasMessages()) {
$messages['warning'] = $flashMessenger->getMessages();
}
$flashMessenger->clearMessages();
$flashMessenger->setNamespace('danger');
if ($flashMessenger->hasMessages()) {
$messages['danger'] = $flashMessenger->getMessages();
}
$flashMessenger->clearMessages();
$e->getViewModel()->setVariable('flashMessages', $messages);
});
}