/** * This action is executed before execute any action in the application */ public function beforeDispatch(Phalcon\Events\Event $event, Phalcon\Mvc\Dispatcher $dispatcher) { $auth = $this->session->get('auth'); if (!$auth) { $role = 'Guests'; } else { $role = 'Users'; } $controller = $dispatcher->getControllerName(); $action = $dispatcher->getActionName(); $acl = $this->getAcl(); $allowed = $acl->isAllowed($role, $controller, $action); if ($allowed != Phalcon\Acl::ALLOW) { $this->flash->error("You don't have access to this module"); $dispatcher->forward(array('controller' => 'index', 'action' => 'index')); return false; } }
if (!isset($_GET['_url'])) { $router->setUriSource(Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); } // Fetch routes from user require APPPATH . '/config/routes.php'; // Inject return $router; }); /* |-------------------------------------------------------------------------- | Dispatcher |-------------------------------------------------------------------------- | | It registers the dispatcher as service and will attach event on him to create | 404 system if the controller/action is not found | */ $di->set('dispatcher', function () use($di) { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $evManager = $di->getShared('eventsManager'); $evManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) use($di) { switch ($exception->getCode()) { case Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'errors', 'action' => 'show404')); return FALSE; } }); $dispatcher->setEventsManager($evManager); return $dispatcher; }, TRUE);
return $view; }); $di->setShared('db', function () use($config, $di) { $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "charset" => $config->database->charset)); return $connection; }); $di->set('dispatcher', function () use($di) { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $eventsManager = $di->getShared('eventsManager'); if (ENVIRONMENT == ENVIRONMENT_PRODUCTION) { $eventsManager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) { switch ($exception->getCode()) { case 404: case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array("namespace" => 'App\\Controllers', 'controller' => 'error', 'action' => 'notFound')); return false; break; default: $dispatcher->forward(array("namespace" => 'App\\Controllers', 'controller' => 'error', 'action' => 'uncaughtException')); return false; break; } }); } $eventsManager->attach('dispatch', new \App\Library\Zonner()); $dispatcher->setEventsManager($eventsManager); $dispatcher->setDefaultNamespace('App\\Controllers'); $dispatcher->setDefaultController('index'); $dispatcher->setDefaultAction('index'); return $dispatcher;
/** * This action is executed before execute any action in the application */ public function beforeDispatch(Phalcon\Events\Event $event, Phalcon\Mvc\Dispatcher $dispatcher) { $auth = $this->session->get('auth'); if (!$auth) { $auth['role'] = 'Guest'; $role = 'Guest'; } else { $role = $auth['role']; } $controller = $dispatcher->getControllerName(); $action = $dispatcher->getActionName(); $acl = $this->getAcl(); $allowed = $acl->isAllowed($role, $controller, $action); if ($role == 'Admin' || $role == 'User' || $role == 'Guest') { return true; } elseif ($allowed != Phalcon\Acl::ALLOW) { if ($role != 'Guest') { $this->flash->error("You don't have access to {$controller}/{$action}) please login to get access"); } $dispatcher->forward(array('namespace' => 'PRIME\\Controllers', 'controller' => 'session', 'action' => 'index')); return false; } }