public function afterExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { $view = $dispatcher->getDI()->getService('view')->resolve(); $viewHelper = $dispatcher->getDI()->get(ViewHelper::SERVICE_NAME); if ($viewHelper) { $view->setVar('helper', $viewHelper); } }
/** * @param Event $event * @param MvcDispatcher $dispatcher */ public function beforeDispatchLoop(Event $event, MvcDispatcher $dispatcher) { if (!$dispatcher->getDI()->has('request')) { return; } /** @var \Phalcon\Http\Request $request */ $request = $dispatcher->getDI()->get('request'); if ($request->isAjax()) { /** @var MvcView $view */ $view = $dispatcher->getDI()->get('view'); $view->setRenderLevel(MvcView::LEVEL_ACTION_VIEW); } }
public function beforeExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { /** @var \Phalcon\Http\Request $request */ $request = $dispatcher->getDI()->getService('request')->resolve(); if ($dispatcher->getParam('lang')) { $lang = $dispatcher->getParam('lang'); } else { $lang = $request->getBestLanguage(); } $view = $dispatcher->getDI()->getService('view')->resolve(); $translationHelper = $dispatcher->getDI()->get(\Sb\Phalcon\Helpers\TranslationHelper::SERVICE_NAME); $translationHelper->setLang($lang); $view->setVar('t', $translationHelper->getTranslation()); }
/** * beforeDispatchLoop * * @param Event $event * @param Dispatcher $dispatcher * @return \Phalcon\Http\ResponseInterface */ public function beforeDispatchLoop(Event $event, Dispatcher $dispatcher) { if ($this->auth->hasRememberMe()) { $this->auth->loginWithRememberMe(false); } if ($this->auth->isUserSignedIn()) { $actionName = $dispatcher->getActionName(); $controllerName = $dispatcher->getControllerName(); if ($controllerName == 'user' && $actionName == 'login') { return $this->response->redirect($config->pup->redirect->success); } } $config = $dispatcher->getDI()->get('config'); $pupConfig = $this->getConfigStructure($config); $needsIdentity = $this->needsIdentity($pupConfig, $dispatcher); $identity = $this->auth->getIdentity(); if (true === $needsIdentity) { if (!is_array($identity)) { $this->flash->notice('Private area. Please login.'); $this->view->disable(); return $this->response->redirect($config->pup->redirect->failure)->send(); } } $this->view->setVar('identity', $identity); }
/** * Executes before executing any route. * Handles sign up policy. Blocks page if open sign up disabled by configuration. * @param \Phalcon\Mvc\Dispatcher $dispatcher Phalcon application dispatcher instance * @return void */ public function beforeExecuteRoute($dispatcher) { $di = $dispatcher->getDI(); $config = $di->get('config'); if ($dispatcher->getActionName() == "signup" && !$config->information->openSignUp) { return false; } }
public function afterExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { /** @var \Phalcon\Mvc\Router $route */ $route = $dispatcher->getDI()->get('router'); $view = $dispatcher->getDI()->getService('view')->resolve(); $wasForwarded = $dispatcher->wasForwarded(); if ($wasForwarded) { return; } if ($route->getMatchedRoute()) { $paths = $route->getMatchedRoute()->getPaths(); $controller = $paths['controller']; $action = \Phalcon\Text::uncamelize($paths['action']); $action = str_replace("_", "-", $action); $template = $controller . '/' . $action; $view->pick([$template]); } }
public function afterExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { $view = $dispatcher->getDI()->getService('view')->resolve(); $controllerData = $event->getData(); if (is_array($controllerData)) { foreach ($controllerData as $key => $value) { $view->setVar($key, $value); } } }
/** * 校验拦截器,用以传入参数的校验。 * @param \Phalcon\Events\Event $event * @param \Phalcon\Mvc\Dispatcher $dispatcher * @param array $params * @return mixed */ public function action($event, $dispatcher, $params) { //$module = $dispatcher->getModuleName(); //$controller = $dispatcher->getControllerName(); $action = $dispatcher->getActionName(); if ($action == 'auth') { return; } $auth = $dispatcher->getDI()->get(AppConstant::DI_SERVICE_SESSION)->get(AppConstant::HTTP_USER_AUTH); if (!$auth) { ResponseApi::sendErrorAndEnd(null, Message::$_ERROR_UNLOGIN); } }
/** * @param array $config * @return MvcDispatcher */ public static function createMvcFrom(array $config) : MvcDispatcher { $dispatcher = new MvcDispatcher(); $dispatcher->setEventsManager(new EventManager()); $dispatcher->setControllerSuffix(null); $dispatcher->setDefaultNamespace($config['controllerDefaultNamespace']); $dispatcher->getEventsManager()->attach('dispatch:afterDispatchLoop', function (Event $event, MvcDispatcher $dispatcher) { if ($dispatcher->getReturnedValue() instanceof \Phalcon\Http\Response) { $dispatcher->getDI()->setShared('response', $dispatcher->getReturnedValue()); } }); return $dispatcher; }
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); }
public function afterExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { /** @var \Phalcon\Mvc\View $view */ $view = $dispatcher->getDI()->getService('view')->resolve(); $returnedValue = $dispatcher->getReturnedValue(); if ($returnedValue instanceof \Phalcon\Http\Response) { return false; } if (is_array($view->getParamsToView()) && array_key_exists(self::SET_JSON_RESPONSE, $view->getParamsToView())) { $view->disable(); header('Content-type: application/json'); echo json_encode($returnedValue); return false; } if (is_array($returnedValue)) { $view->setVars($returnedValue); } }
public function __construct(Dispatcher $dispatcher, array $modules, $defaultLanguage = 'en') { $this->lang = $dispatcher->getParam('language'); if (is_null($this->lang)) { $this->lang = $defaultLanguage; } $config = $dispatcher->getDI()->get('config'); $translations = $this->getMessages($config->projectPath . 'common/'); if (!is_array($translations)) { $translations = []; } $translationsModule = $this->getMessages($modules[$dispatcher->getModuleName()]); if (!is_array($translationsModule)) { $translationsModule = []; } $translations = array_merge($translations, $translationsModule); $this->translate = new NativeArray(['content' => $translations]); }
/** * @param \Phalcon\Mvc\Dispatcher|\Phalcon\Mvc\DispatcherInterface $dispatcher * @return bool */ public function beforeExecuteRoute($dispatcher) { $this->operator = new OperatorIdentity(); $this->operator->cookies = $this->cookies; $this->operator->load(); // 判断登录 $moduleConfig = $this->di->get('moduleConfig'); $controllerName = $dispatcher->getControllerName(); $actionName = $dispatcher->getActionName(); if ($controllerName == $moduleConfig->operator->login->controller && $actionName == $moduleConfig->operator->login->action) { return true; } if ($this->operator->isLogin()) { // 判断权限 $result = OperatorGroupRight::verify($this->operator->group, $controllerName, $actionName, $dispatcher->getDI()); // 权限不足,输出错误 if (!$result) { $content = ''; if ($this->request->isAjax()) { $resp = new ResponseResult(); $resp->sendError(ResponseResultStatus::RIGHT_VERIFY_FAIL, '权限不足!'); $content = $resp->getContent(); } else { $content = '访问权限不足!'; } $this->view->disable(); echo $content; } return $result; } if ($this->request->isAjax()) { $resp = new ResponseResult(); $resp->sendError(ResponseResultStatus::USER_TOKEN_EXPIRES, '未登录!'); echo $resp->getContent(); } else { $this->response->redirect($moduleConfig->operator->login->url); } $this->view->disable(); return false; }
public function testModelBinding(IntegrationTester $I) { $dispatcher = new Dispatcher(); $dispatcher->setModelBinding(true); $dispatcher->setDI(Di::getDefault()); $I->assertInstanceOf('Phalcon\\Di', $dispatcher->getDI()); $I->haveServiceInDi('dispatcher', $dispatcher); // Model to test with $model = People::findFirst(); $dispatcher->setControllerName('test10'); $dispatcher->setActionName('view'); $dispatcher->setParams([$model->cedula]); $dispatcher->dispatch(); $I->assertInstanceOf('Phalcon\\Test\\Models\\People', $dispatcher->getReturnedValue()); $I->assertEquals($model->cedula, $dispatcher->getReturnedValue()->cedula); // Reset dispatcher $dispatcher = new Dispatcher(); $dispatcher->setDI(Di::getDefault()); $I->assertInstanceOf('Phalcon\\Di', $dispatcher->getDI()); $I->haveServiceInDi('dispatcher', $dispatcher); $dispatcher->setControllerName('test9'); $dispatcher->setActionName('view'); $dispatcher->setParams([$model->cedula]); try { $dispatcher->dispatch(); $I->assertTrue(false, 'Here must be the exception about passing non model to the controller action'); } catch (\Exception $e) { // PHP 5.x $I->assertEquals('Argument 1 passed to Test9Controller::viewAction() must be an instance of Phalcon\\Mvc\\Model, string given', $e->getMessage()); } catch (\TypeError $e) { // PHP 7.x $I->assertEquals('Argument 1 passed to Test9Controller::viewAction() must be an instance of Phalcon\\Mvc\\Model, string given', $e->getMessage()); } $dispatcher->setModelBinding(true); $dispatcher->dispatch(); $I->assertInstanceOf('Phalcon\\Test\\Models\\People', $dispatcher->getReturnedValue()); $I->assertEquals($dispatcher->getReturnedValue()->cedula, $model->cedula); }
/** * @param Dispatcher $dispatcher * * @return View */ public function getView(Dispatcher $dispatcher) { return $dispatcher->getDI()->getShared('view'); }
public function testDispatcher() { $this->specify("Dispatcher doesn't throw exception when handler can't be loaded", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("index"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "IndexController handler class cannot be loaded"]]); $this->specify("Dispatcher doesn't throw exception when handler can't be loaded (2)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("essai"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "EssaiController handler class cannot be loaded"]]); $this->specify("Dispatcher doesn't throw exception when handler can't be loaded (3)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test0"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Test0Controller handler class cannot be loaded"]]); $this->specify("Dispatcher doesn't throw exception when action can't be found", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test1"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'index' was not found on handler 'test1'"]]); $this->specify("Dispatcher doesn't return controller after dispatching", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("index"); $dispatcher->setParams([]); $controller = $dispatcher->dispatch(); expect($controller)->isInstanceOf("Test2Controller"); }); $this->specify("Dispatcher doesn't throw exception when action can't be found (2)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("essai"); $dispatcher->setParams([]); $dispatcher->dispatch(); }, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'essai' was not found on handler 'test2'"]]); $this->specify("Dispatcher doesn't work as expected", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("other"); $dispatcher->setParams([]); $controller = $dispatcher->dispatch(); expect($controller)->isInstanceOf("Test2Controller"); }); $this->specify("Dispatcher doesn't work as expected (2)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("another"); $dispatcher->setParams([]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals(100); }); $this->specify("Dispatcher doesn't work as expected (3)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("anotherTwo"); $dispatcher->setParams([2, "3"]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals(5); }); $this->specify("Dispatcher doesn't work as expected (4)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("anotherthree"); $dispatcher->setParams([]); $dispatcher->dispatch(); $value = $dispatcher->getActionName(); expect($value)->equals("anotherfour"); $value = $dispatcher->getReturnedValue(); expect($value)->equals(120); }); $this->specify("Dispatcher doesn't work as expected (5)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test2"); $dispatcher->setActionName("anotherFive"); $dispatcher->setParams(["param1" => 2, "param2" => 3]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals(5); }); $this->specify("Dispatcher doesn't work as expected (6)", function () { $di = new Di(); $di->set("response", new Response()); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di"); $di->set("dispatcher", $dispatcher); $dispatcher->setControllerName("test7"); $dispatcher->setActionName("service"); $dispatcher->setParams([]); $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); expect($value)->equals("hello"); $value = $dispatcher->getControllerClass(); expect($value)->equals("Test7Controller"); }); }
public function afterExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { $view = $dispatcher->getDI()->getService('view')->resolve(); $view->setVar('url', new \PhalconEx\Helpers\UrlHelper($dispatcher->getDI())); }
/** * 在执行控制器/动作方法后触发。由于此操作不可终止,所以仅在执行动作后才使用此事件进行清理工作。 * * @param Event $event * @param Dispatcher $dispatcher * * @author Hunter.<*****@*****.**> */ public function afterExecuteRoute(Event $event, Dispatcher $dispatcher) { //清理 $dispatcher->getDI()->get('session')->remove('error_exception_code'); //$dispatcher->getDI()->get('session')->remove('error_redirect_url'); }