本文整理汇总了PHP中Zend\Mvc\MvcEvent::getRouter方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::getRouter方法的具体用法?PHP MvcEvent::getRouter怎么用?PHP MvcEvent::getRouter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::getRouter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
public function __invoke(MvcEvent $event)
{
if ($event->getRouteMatch()->getMatchedRouteName() === 'oauth/authorize' || $event->getRouteMatch()->getMatchedRouteName() === 'oauth/code') {
$auth = $this->authentication;
if (!$auth->hasIdentity()) {
//redirect to login form before granting permissions - exception would be client_credentials grant type
$url = $event->getRouter()->assemble([], array('name' => 'dotuser/login'));
$host = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'on' ? 'https://' : 'http://';
$host .= $_SERVER['HTTP_HOST'];
$url = $host . $url . '?redirect=' . urlencode($event->getRequest()->getUriString());
$response = $event->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
exit;
} else {
$identity = $auth->getIdentity();
$request = $event->getRequest();
$client_id = $request->getQuery('client_id');
//check to see if user already ganted permissions and is not revoked to skip the step and redirecting directly
if ($event->getRouteMatch()->getMatchedRouteName() === 'oauth/authorize') {
if ($this->userRevokeStorage->isAuthorized($client_id, $identity->getUsername())) {
$newRequest = new \ZF\ContentNegotiation\Request();
$newRequest->setMethod(\Zend\Http\Request::METHOD_POST);
$newRequest->getPost()->set('authorized', 'yes');
$event->setRequest($newRequest);
}
}
}
}
}
示例2: preDispatch
/**
* preDispatch Event Handler
* Handle authentication process
* Decide where user should be redirected to when logged in or not
*
*
* @access public
* @uses AuthenticationService
* @uses Response
*
* @param \Zend\Mvc\MvcEvent $event
* @throws \Exception
*/
public function preDispatch(MvcEvent $event)
{
// ACL dispatcher is used only in HTTP requests not console requests
if (!$event->getRequest() instanceof HttpRequest) {
return;
}
$userAuth = new AuthenticationService();
$user = array();
$signInController = 'DefaultModule\\Controller\\Sign';
if ($userAuth->hasIdentity()) {
$user = $userAuth->getIdentity();
}
$routeMatch = $event->getRouteMatch();
$controller = $routeMatch->getParam('controller');
$action = $routeMatch->getParam('action');
if ($userAuth->hasIdentity() && isset($user['status']) && $user['status'] == 2) {
$userAuth->clearIdentity();
// redirect to sign/out
$url = $event->getRouter()->assemble(array('action' => 'out'), array('name' => 'defaultSign'));
} else {
if ($userAuth->hasIdentity() && $controller == $signInController && $action == 'in') {
// redirect to index
$url = $event->getRouter()->assemble(array('action' => 'index'), array('name' => 'home'));
}
}
if (isset($url)) {
$event->setResponse(new Response());
$this->redirect()->getController()->setEvent($event);
$response = $this->redirect()->toUrl($url);
return $response;
}
}
示例3: onRoute
public function onRoute(MvcEvent $e)
{
$serviceManager = $e->getApplication()->getServiceManager();
$routeMatchName = $e->getRouteMatch()->getMatchedRouteName();
if (strpos($routeMatchName, '.rest.') !== false || strpos($routeMatchName, '.rpc.') !== false) {
return;
}
$config = $serviceManager->get('Config');
$identityGuards = $config['zource_guard']['identity'];
$needsIdentity = null;
foreach ($identityGuards as $guard => $needed) {
if (fnmatch($guard, $routeMatchName)) {
$needsIdentity = $needed;
break;
}
}
if ($needsIdentity === null) {
throw new RuntimeException(sprintf('The identity guard "%s" has not been configured.', $routeMatchName));
}
if (!$needsIdentity) {
return;
}
$authenticationService = $serviceManager->get('Zend\\Authentication\\AuthenticationService');
if ($authenticationService->hasIdentity()) {
return;
}
$returnUrl = $e->getRouter()->assemble([], ['name' => $routeMatchName, 'force_canonical' => true, 'query' => $e->getRequest()->getUri()->getQuery()]);
$url = $e->getRouter()->assemble([], ['name' => 'login', 'query' => ['redir' => $returnUrl]]);
$response = new Response();
$response->setStatusCode(Response::STATUS_CODE_302);
$response->getHeaders()->addHeaderLine('Location: ' . $url);
return $response;
}
示例4: 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);
}
示例5: dispatchEvent
private function dispatchEvent(MvcEvent $e)
{
// TARGET & SERVICEMANAGER
$target = $e->getTarget();
$serviceManager = $e->getApplication()->getServiceManager();
// CONFIG
$config = $e->getApplication()->getServiceManager()->get('config');
// TRANSLATOR
/** @var Translator $translator */
$translator = $serviceManager->get('MvcTranslator');
// LANG CONFIG
$languages = $config['languages']['available'];
$fallbackLocale = $config['languages']['fallback'];
// ROUTER PARAM LOCALE
$routeMatch = $e->getRouteMatch();
if ($routeMatch) {
$urlLocale = $routeMatch->getParam('locale');
} else {
/**
* @todo
* evtl hier noch aus der request url versuchen die locale zu lesen
*/
$urlLocale = $fallbackLocale;
}
if (strstr($_SERVER['SERVER_NAME'], '.de')) {
$urlLocale = 'de';
} else {
$urlLocale = 'en';
}
// ROUTER
/** @var \Zend\Mvc\Router\Http\TreeRouteStack $router */
$router = $e->getRouter();
// check if language is present
$usedLocale = isset($languages[$urlLocale]) ? $languages[$urlLocale] : false;
$usedLocaleShort = isset($languages[$urlLocale]) ? $urlLocale : false;
// language not found redirect to fallback lang
if (!$usedLocale && !$usedLocaleShort) {
// wrong locale request
$e->getRouteMatch()->setParam('locale', $fallbackLocale);
$url = $e->getRouter()->assemble($e->getRouteMatch()->getParams(), array('name' => $e->getRouteMatch()->getMatchedRouteName()));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
return $response;
}
// add current locale as default router param
$router->setDefaultParam('locale', $usedLocaleShort);
$translator->setLocale($usedLocale);
$translator->setFallbackLocale($usedLocale);
$translator->addTranslationFile('phpArray', 'vendor/zendframework/zendframework/resources/languages/' . $usedLocaleShort . '/Zend_Validate.php', 'default', $usedLocale);
\Locale::setDefault($usedLocale);
AbstractValidator::setDefaultTranslator($translator);
}
示例6: preDispatch
/**
* @param MvcEvent $e
* @throws \Exception
* @throws \ServiceLocatorFactory\NullServiceLocatorException
*/
public function preDispatch(MvcEvent $e)
{
$application = $e->getApplication();
$sm = $application->getServiceManager();
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
$params = $matchedRoute->getParams();
$controller = $params['controller'];
if (!isset($controller)) {
return false;
}
$currentControllerNamespace = explode('\\', $controller);
if ($currentControllerNamespace[0] == 'Admin') {
$sl = ServiceLocatorFactory::getInstance();
$session = new SessionContainer();
$userDetails = $session->offsetGet('userDetails');
/* Check Admin Area login */
if (!$sl->get('AuthService')->hasIdentity() or $userDetails->sitename != $this->recoverSitename($sl)) {
$url = $e->getRouter()->assemble(array('action' => 'index'), array('name' => 'login'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
// Check ACL
$roles = (include __DIR__ . '/config/module.acl.roles.php');
foreach ($roles as $key => $value) {
if ($key == $matchedRoute->getMatchedRouteName()) {
if (isset($value['resources'])) {
$allowed = 0;
foreach ($value['resources'] as $resource) {
if ($userDetails->acl->hasResource($resource)) {
$allowed = 1;
}
}
/* No permissions, redirect... */
if ($allowed == 0) {
$url = $e->getRouter()->assemble(array('lang' => 'it'), array('name' => 'admin/not-authorized'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(401);
$response->sendHeaders();
exit;
}
}
}
}
}
}
示例7: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$serviceManager = $e->getApplication()->getServiceManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$serviceManager->get('LocaleManager')->setLocale('es_ES');
if ($serviceManager->has('Translator') && $e->getRouter() instanceof TranslatorAwareInterface) {
$translator = $serviceManager->get('Translator');
if ($translator instanceof TranslatorInterface) {
$e->getRouter()->setTranslator($translator);
$e->getRouter()->setTranslatorTextDomain('Zend\\Mvc\\Router');
}
}
}
示例8: onBootstrap
public function onBootstrap(MvcEvent $e)
{
$this->initSession();
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$list = array();
$auth = $e->getApplication()->getServiceManager()->get("Zend\\Authentication\\AuthenticationService");
$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;
}
// Route is whitelisted
$route = $match->getMatchedRouteName();
$params = $match->getParams();
$module = explode('/', $route);
/*
if($_SERVER['REMOTE_ADDR'] != '127.0.0.1' && $_SERVER['SERVER_PORT'] != '443') {
$url = 'Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header(str_replace( 'www.', '' , $url));
exit();
}*/
if (@$module[0] == 'login') {
if ($auth->hasIdentity()) {
$router = $e->getRouter();
$url = $router->assemble(array(), array('name' => 'messages'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
} else {
return;
}
}
if ($auth->hasIdentity()) {
return;
} else {
$router = $e->getRouter();
$url = $router->assemble(array(), array('name' => 'login'));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
}, -100);
}
示例9: onError
/**
* @private
* @param MvcEvent $event
* @return void
*/
public function onError(MvcEvent $event)
{
// Do nothing if no error or if response is not HTTP response
if (!$event->getParam('exception') instanceof UnauthorizedExceptionInterface || $event->getResult() instanceof HttpResponse || !$event->getResponse() instanceof HttpResponse) {
return;
}
$router = $event->getRouter();
if ($this->authenticationService->hasIdentity()) {
if (!$this->options->getRedirectWhenConnected()) {
return;
}
$redirectRoute = $this->options->getRedirectToRouteConnected();
} else {
$redirectRoute = $this->options->getRedirectToRouteDisconnected();
}
$uri = $router->assemble([], ['name' => $redirectRoute]);
if ($this->options->getAppendPreviousUri()) {
$redirectKey = $this->options->getPreviousUriQueryKey();
$previousUri = $event->getRequest()->getUriString();
$uri = $router->assemble([], ['name' => $redirectRoute, 'query' => [$redirectKey => $previousUri]]);
}
$response = $event->getResponse() ?: new HttpResponse();
$response->getHeaders()->addHeaderLine('Location', $uri);
$response->setStatusCode(302);
$event->setResponse($response);
$event->setResult($response);
}
示例10: __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);
}
}
示例11: 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;
}
示例12: preDispatch
/**
* preDispatch Event Handler
*
* @param \Zend\Mvc\MvcEvent $event
* @throws \Exception
*/
public function preDispatch(MvcEvent $event)
{
//@todo - Should we really use here and Controller Plugin?
$userAuth = $this->getUserAuthenticationPlugin();
$acl = $this->getAclClass();
$role = AclClass::DEFAULT_ROLE;
if ($userAuth->hasIdentity()) {
$user = $userAuth->getIdentity();
$role = 'member';
//@todo - Get role from user!
}
$routeMatch = $event->getRouteMatch();
$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 = $event->getRouter()->assemble(array(), array('name' => 'login'));
$response = $event->getResponse();
$response->headers()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
}
示例13: 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;
}
示例14: handleError
/**
* @param MvcEvent $e
*/
protected function handleError(MvcEvent $e)
{
$router = $e->getRouter();
if ($this->authenticationService->hasIdentity()) {
if (!$this->options->getRedirectWhenConnected()) {
return;
}
$redirectRoute = $this->options->getRedirectToRouteConnected();
} else {
$redirectRoute = $this->options->getRedirectToRouteDisconnected();
}
$params = array();
$options = array('name' => $redirectRoute);
if ($this->options->getAppendPreviousUri()) {
$redirectKey = $this->options->getPreviousUriRouteKey();
$previousUri = $e->getRequest()->getUriString();
$params = array($redirectKey => $previousUri);
}
$uri = $router->assemble($params, $options);
$response = $e->getResponse() ?: new HttpResponse();
$response->getHeaders()->addHeaderLine('Location', $uri);
$response->setStatusCode(302);
$e->setResponse($response);
$e->setResult($response);
}
示例15: restrictAccess
public function restrictAccess(MvcEvent $e, array $whiteListed = array(), $loginRouteName = 'admin/login')
{
// $loginRouteName is whitelisted from the redirection
// The redirection is to the $loginRouteName
// additional routes can be whitelisted
if (!$e->getApplication()->getServiceManager()->get('Zend\\Authentication\\AuthenticationService')->hasIdentity()) {
$whiteListed[] = $loginRouteName;
$match = $e->getRouteMatch();
// No route match, this is a 404
if (!$match instanceof RouteMatch) {
return;
}
// Login route is whitelisted
if (in_array($match->getMatchedRouteName(), $whiteListed)) {
return;
}
// login url from login route
$url = $e->getRouter()->assemble(array(), array('name' => $loginRouteName));
// redirect response
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
exit;
}
}