/**
  * @param Dispatcher $dispatcher
  *
  * @return bool
  */
 public function beforeExecuteRoute(Dispatcher $dispatcher)
 {
     $returnVal = true;
     $lang = $this->getUriParameter('language');
     $controllerName = $dispatcher->getControllerName();
     if ('1' != $this->config->application->debug) {
         $lang = $this->getUriParameter('language');
         $lang = $lang ? $lang : 'en';
         $key = preg_replace('/[^a-zA-Z0-9\\_]/', '', $lang . '-' . $dispatcher->getControllerName() . '-' . $dispatcher->getActionName() . '-' . implode('-', $dispatcher->getParams()));
         $this->view->cache(array('key' => $key));
         if ($this->view->getCache()->exists($key)) {
             $returnVal = false;
         }
     }
     $auth = $this->session->get('auth');
     $identity = $this->auth->getIdentity();
     if (!$auth) {
         $role = 'Guests';
     } else {
         $role = $identity['profile'];
     }
     // Check if the user have permission to the current option
     $actionName = $dispatcher->getActionName();
     if (!$this->acl->isAllowed($role, $controllerName, $actionName)) {
         $this->flash->notice('You don\'t have access to this module: ' . $controllerName . ':' . $actionName);
         if ($this->acl->isAllowed($identity['profile'], $controllerName, 'index')) {
             $dispatcher->forward(array('controller' => $controllerName, 'action' => 'index'));
         }
         $returnVal = false;
     } else {
         $this->requestInitialize($controllerName);
     }
     return $returnVal;
 }
Пример #2
0
 /**
  * Generate a route based on the current URL.
  *
  * @param $path_info
  * @return string The routed URL.
  */
 public function routeFromHere($path_info)
 {
     $new_path = array('module' => $this->_dispatcher->getModuleName(), 'controller' => $this->_dispatcher->getControllerName(), 'action' => $this->_dispatcher->getActionName(), 'params' => (array) $this->_dispatcher->getParams());
     if (isset($path_info['module'])) {
         $new_path['module'] = $path_info['module'];
         unset($path_info['module']);
     }
     if (isset($path_info['controller'])) {
         $new_path['controller'] = $path_info['controller'];
         unset($path_info['controller']);
     }
     if (isset($path_info['action'])) {
         $new_path['action'] = $path_info['action'];
         unset($path_info['action']);
     }
     if (count($path_info) > 0) {
         foreach ((array) $path_info as $param_key => $param_value) {
             $new_path['params'][$param_key] = $param_value;
         }
     }
     if (isset($new_path['params']['name'])) {
         // Allow support for named routes.
         $route_name = $new_path['params']['name'];
         unset($new_path['params']['name']);
         return $this->named($route_name, $new_path['params']);
     } else {
         return $this->route($new_path);
     }
 }
Пример #3
0
 /**
  * @param Dispatcher $dispatcher
  */
 public function beforeExecuteRoute(Dispatcher $dispatcher)
 {
     $aParams = $dispatcher->getParams();
     $aNewParams = array();
     for ($i = 0; $i < count($aParams); $i = $i + 2) {
         if (isset($aParams[$i + 1])) {
             $aNewParams[$aParams[$i]] = $aParams[$i + 1];
         }
     }
     $dispatcher->setParams(array_merge($aNewParams, $_GET, $this->getPost()));
 }
Пример #4
0
 /**
  * @param Dispatcher $dispatcher
  *
  * @return bool
  */
 public function beforeExecuteRoute(Dispatcher $dispatcher)
 {
     if (!$this->config->application->debug) {
         $lang = $this->getLang();
         $key = preg_replace('/[^a-zA-Z0-9\\_]/', '', $lang . '-' . $dispatcher->getControllerName() . '-' . $dispatcher->getActionName() . '-' . implode('-', $dispatcher->getParams()));
         $this->view->cache(array('key' => $key));
         if ($this->view->getCache()->exists($key)) {
             return false;
         }
     }
     $this->requestInitialize();
     return true;
 }
Пример #5
0
 public function afterExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher)
 {
     /** @var \Model\ModelsRepository $modelsRepository */
     $modelsRepository = $dispatcher->getDI()->getService('modelsRepository')->resolve();
     if (!$modelsRepository) {
         return;
     }
     $returnedValue = $dispatcher->getReturnedValue();
     if (is_null($returnedValue)) {
         $returnedValue = [];
     }
     $modelsRepository->getPageContent()->processContent($dispatcher->getParams(), $returnedValue);
 }
Пример #6
0
 /**
  * puts any params in the url into an assoc array in the dispatcher
  * for example given this url
  *		/en/employer/profile/edit/id/5/company/6/email/test@test.com
  * then in the controller
  *		$this->dispatcher->getParam('id') = 5
  *		$this->dispatcher->getParam('company') = 6
  *		$this->dispatcher->getParam('email') = test@test.com
  * @param Event $event
  * @param Dispatcher $dispatcher
  */
 public function beforeDispatch(Event $event, Dispatcher $dispatcher)
 {
     $key_params = array();
     $params = $dispatcher->getParams();
     foreach ($params as $number => $value) {
         if ($number & 1) {
             $key_params[$params[$number - 1]] = $value;
         }
     }
     //loop again so we don't overwrite any params named in the route (like 'lang')
     foreach ($key_params as $param => $value) {
         if ($dispatcher->getParam($param) === null) {
             $dispatcher->setParam($param, $value);
         }
     }
 }
Пример #7
0
 /**
  * Register the services here to make them general or register in the
  * ModuleDefinition to make them module-specific
  */
 public function registerServices($di)
 {
     //Registering a dispatcher
     $di['dispatcher'] = function () {
         $dispatcher = new PhDispatcher();
         //Attach a event listener to the dispatcher
         $eventManager = new PhEveManager();
         //Notfound redirect
         // $eventManager->attach('dispatch:beforeException', function($event, $dispatcher, $exception) {
         //     //Alternative way, controller or action doesn't exist
         //     if ($event->getType() == 'beforeException') {
         //         switch ($exception->getCode()) {
         //             case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND:
         //             case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND:
         //                 $dispatcher->forward([
         //                     'module' => 'admin',
         //                     'controller' => 'notfound'
         //                 ]);
         //                 return false;
         //         }
         //     }
         // });
         //attach get param after controller as key/value
         $eventManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) {
             $keyParams = [];
             $params = $dispatcher->getParams();
             //Use odd parameters as keys and even as values
             foreach ($params as $number => $value) {
                 if ($number & 1) {
                     $keyParams[$params[$number - 1]] = $value;
                 }
             }
             //Override parameters
             $dispatcher->setParams($keyParams);
         });
         // Authorization
         $eventManager->attach('dispatch', new \Fly\Authorization('admin'));
         $dispatcher->setEventsManager($eventManager);
         $dispatcher->setDefaultNamespace('Controller\\Admin');
         return $dispatcher;
     };
     $di['view']->setViewsDir(ROOT_PATH . '/modules/admin/views/');
 }
 /**
  * registering module-specific services
  *
  * @param \DiCustom $di
  */
 public function registerServices(DiInterface $di)
 {
     $oLogger = $di->getFileLogger();
     //		$oRouter = new Router(false);
     $oRouter = new CustomRouter(false);
     //		$oOldRouter = $di->getRouter();
     //		Tester::ec('old router: ' . HC::className($oOldRouter));
     $di->set('router', $oRouter);
     //		Tester::ec('new router set: ' . HC::className($di->getRouter()));
     $oRouter->mount(new ApiRoutes($di));
     $oVersionLoader = new VersionLoader();
     $di->set('versionLoader', $oVersionLoader);
     //		$oApiDispatcherEventsManager = new Manager();
     ////		$oLogger = $di->getFileLogger();
     //		$oRouter = $di->getRouter();
     //
     //		$oLogger->debug('api module ' . __FUNCTION__ . ': setting up dispatcher');
     //
     //		$oApiDispatcherEventsManager->attach('dispatch', function(Event $event, Dispatcher $dispatcher, $data) use($oLogger, $oRouter){
     //			$oLogger->debug('api dispatcher: ' . $event->getType() . ': ' . print_r($oRouter->getMatchedRoute(), true));
     //		});
     //
     //
     //		$oDispatcher = $di->getDispatcher();
     //		$oDispatcher->setDefaultNamespace('App\Modules\Api\Web');
     //		$oDispatcher->setControllerSuffix('Homorrag');
     //		$oDispatcher->setEventsManager($oApiDispatcherEventsManager);
     $oDispatcher = new Dispatcher();
     $oApiDispatcherEventsManager = new Manager();
     $oApiDispatcherEventsManager->attach('dispatch:beforeDispatchLoop', function (Event $oEvent, Dispatcher $oDispatcher, $data) {
         /**
          * @type \DiCustom $di
          */
         $di = Di::getDefault();
         $oLogger = $di->getFileLogger();
         $arParams = $oDispatcher->getParams();
         $oLogger->debug(__CLASS__ . ': ' . $oEvent->getType() . ': trying to dispatch:' . ' module: ' . $oDispatcher->getModuleName() . ' media: ' . $arParams['media'] . ' version: v' . $arParams['major'] . '_' . $arParams['minor'] . ' controller: ' . $oDispatcher->getControllerName() . ' action: ' . $oDispatcher->getActionName());
         $di->getVersionLoader()->load();
     });
     $oApiDispatcherEventsManager->attach('dispatch', function (Event $oEvent, Dispatcher $oDispatcher, $data) {
         /**
          * @type \DiCustom $di
          */
         $di = Di::getDefault();
         $oLogger = $di->getFileLogger();
         //			$oRouter = $di->getRouter();
         //
         //			$arParams = $oRouter->getParams();
         //
         //			$oLogger->debug(__CLASS__ . ': ' . $oEvent->getType() . ': trying to dispatch: from router: '
         //				. ' module: ' . $oRouter->getModuleName()
         //				. ' media: ' . $arParams['media']
         //				. ' version: v' . $arParams['major'] . '_' . $arParams['minor']
         //				. ' controller: ' . $oRouter->getControllerName()
         //				. ' action: ' . $oRouter->getActionName()
         //			);
         $arParams = $oDispatcher->getParams();
         $oLogger->debug(__CLASS__ . ': ' . $oEvent->getType() . ': trying to dispatch: from dispatcher: ' . ' module: ' . $oDispatcher->getModuleName() . ' media: ' . $arParams['media'] . ' version: v' . $arParams['major'] . '_' . $arParams['minor'] . ' controller: ' . $oDispatcher->getControllerName() . ' action: ' . $oDispatcher->getActionName());
         //			$oLogger->debug(__CLASS__ . ': ' . $oEvent->getType());
     });
     $oDispatcher->setEventsManager($oApiDispatcherEventsManager);
     $di->setShared('dispatcher', $oDispatcher);
     //		$di->set('dispatcher', function() use($di){
     //			$dispatcher = new Dispatcher();
     //			$oApiDispatcherEventsManager = new Manager();
     //			$oLogger = $di->getFileLogger();
     //			$oRouter = $di->getRouter();
     //			$oRequest = $di->getRequest();
     //
     //			$oLogger->debug('api module ' . __FUNCTION__ . ': setting up dispatcher');
     //
     //			$oApiDispatcherEventsManager->attach('dispatch', function(Event $event, Dispatcher $dispatcher, $data) use($oLogger, $oRouter, $oRequest){
     //
     //				if($event->getType() == 'beforeDispatchLoop'){
     //
     //					$arRoutes = $oRouter->getRoutes();
     //
     //					foreach ($arRoutes as $oRoute) {
     //						$oRoute->beforeMatch(function($uri, $route) use ($oLogger){
     //							$oLogger->debug('__ api module dispatcher route beforeMatch: ' . $uri . $route);
     //
     //						});
     //						$oLogger->debug('api module dispatcher: ' . $event->getType() . ': route registered: ' . $oRoute->getCompiledPattern());
     //
     //						$regPattern = $oRoute->getCompiledPattern();
     //
     //						$strUri = $oRequest->getURI();
     //
     //						if(preg_match($regPattern, $strUri)){
     //							$oLogger->debug('"' . $strUri . '" matched ' . $regPattern);
     //						}else{
     //							$oLogger->debug('"' . $strUri . '" mismatched ' . $regPattern);
     //						}
     //
     //					}
     //
     //				}
     //
     //				$oLogger->debug('api dispatcher: ' . $event->getType() . ': route matched: ' . print_r($oRouter->getMatchedRoute(), true));
     //				$oLogger->debug('api dispatcher: ' . $event->getType()
     //					. ' module "' . $oRouter->getModuleName()
     //					. '" controller: "' . $oRouter->getControllerName()
     //					. '" action: "' . $oRouter->getActionName() . '"'
     //				);
     //			});
     //
     //			$dispatcher->setEventsManager($oApiDispatcherEventsManager);
     //			$dispatcher->setDefaultNamespace('App\Modules\Api\Web');
     //			return $dispatcher;
     //		});
     //
     //
     //
     //		$oConfig = new Config(array(
     //			'application' => array(
     //				'viewsDir' => __DIR__ . '/views',
     //				'cacheDir' => __DIR__ . '/../../../var/cache/regular'
     //			),
     //		));
     //
     //		$di->get('config')->merge($oConfig);
     /**
      * Setting up the view component
      */
     $di->set('view', function () use($oLogger) {
         $oView = new View();
         $oView->setRenderLevel(View::LEVEL_NO_RENDER);
         $oView->disable();
         $oLogger->debug('view: render level set to disabled');
         //Disable several levels
         //			$view->disableLevel(array(
         //				View::LEVEL_LAYOUT      => true,
         //				View::LEVEL_MAIN_LAYOUT => true
         //			));
         return $oView;
     }, true);
 }
 /**
  * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
  * @param Dispatcher $dispatcher the request dispatcher
  * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
  */
 public function fromDispatcher($dispatcher)
 {
     $items = array($dispatcher->getControllerName(), $dispatcher->getActionName());
     $items = array_merge($items, $dispatcher->getParams());
     return $this->addElements($items);
 }
Пример #10
0
$this->dispatcher->forward(array("action" => "search", "params" => array(1, 2, 3)));
// Forward flow to another action in another controller
$this->dispatcher->forward(array("controller" => "users", "action" => "signin"));
use Phalcon\Events\Manager as EventsManager, Phalcon\Mvc\Dispatcher as MvcDispatcher, Phalcon\Mvc\Dispatcher\Exception as DispatchException;
$di->set('dispatcher', function () {
    //Create an event manager
    $eventsManager = new EventsManager();
    $dispatcher = new MvcDispatcher();
    //Bind the eventsManager to the view component
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
}, true);
// 准备参数(Preparing Parameters)
$eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) {
    $keyParams = array();
    $params = $dispatcher->getParams();
    foreach ($params as $number => $value) {
        /*
                // Use odd parameters as keys and even as values
                if ($number & 1) {
                    $keyParams[$params[$number - 1]] = $value;
                }
                //*/
        // Explode each parameter as key,value pairs
        /*
                $parts = explode(':', $value);
                $keyParams[$parts[0]] = $parts[1];
                //*/
    }
    $dispatcher->setParams($keyParams);
});
 /**
  * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
  * @param Dispatcher $dispatcher the request dispatcher
  * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
  */
 public function fromDispatcher($dispatcher)
 {
     $params = $dispatcher->getParams();
     $action = $dispatcher->getActionName();
     $items = array($dispatcher->getControllerName());
     if (\sizeof($params) > 0 || \strtolower($action) != "index") {
         $items[] = $action;
         foreach ($params as $p) {
             if (\is_object($p) == false) {
                 $items[] = $p;
             }
         }
     }
     return $this->addElements($items);
 }