Exemplo n.º 1
0
 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());
 }
 public function __construct(Dispatcher $dispatcher)
 {
     $request = $this->getDI()->get('request');
     $queryLang = $request->getQuery('lang');
     if (!$queryLang) {
         $lang = $dispatcher->getParam('lang');
     } else {
         $lang = $queryLang;
     }
     switch ($lang) {
         case 'uk':
             define('LANG', 'uk');
             define('LANG_SUFFIX', '_uk');
             define('LANG_URL', '/uk');
             define('LOCALE', 'uk_UA');
             break;
         case 'en':
             define('LANG', 'en');
             define('LANG_SUFFIX', '_en');
             define('LANG_URL', '/en');
             define('LOCALE', 'en_EN');
             break;
         default:
             define('LANG', 'ru');
             define('LANG_SUFFIX', '');
             define('LANG_URL', '/');
             define('LOCALE', 'ru_RU');
     }
     Locale::setDefault(LOCALE);
     $this->getDI()->set('translate', new \Application\Localization\GettextAdapter(array('locale' => LOCALE, 'lang' => LANG, 'file' => 'messages', 'directory' => APPLICATION_PATH . '/lang')));
 }
Exemplo n.º 3
0
 public function __construct(Dispatcher $dispatcher)
 {
     $cmsCache = new CmsCache();
     $languages = $cmsCache->get('languages');
     $defaultLangArray = array_values(array_slice($languages, 0, 1));
     $defaultLang = $defaultLangArray[0];
     $request = $this->getDI()->get('request');
     $queryLang = $request->getQuery('lang');
     if (!$queryLang) {
         $langParam = $dispatcher->getParam('lang');
     } else {
         $langParam = $queryLang;
     }
     if (!$langParam) {
         $langParam = $defaultLang['iso'];
     }
     foreach ($languages as $language) {
         if ($langParam == $language['iso']) {
             define('LANG', $language['iso']);
             define('LANG_URL', '/' . $language['url']);
         }
     }
     if (!defined('LANG')) {
         define('LANG', $defaultLang['iso']);
         \Application\Mvc\Model\Model::$lang = $defaultLang['iso'];
     }
     if (!defined('LANG_URL')) {
         define('LANG_URL', $defaultLang['url']);
     }
     $translations = \Cms\Model\Translate::findCachedByLangInArray(LANG);
     $this->getDI()->set('translate', new \Phalcon\Translate\Adapter\NativeArray(['content' => $translations]));
 }
Exemplo n.º 4
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);
         }
     }
 }
Exemplo n.º 5
0
 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]);
 }
Exemplo n.º 6
0
 public function passThrouthMiddleWares(Request $request, Response $response, Dispatcher $dispatcher)
 {
     $route = $this->getMatchedRoute();
     if (null == $route) {
         die('url is invalid, their is no matched route for this url!');
     }
     $pattern = $route->getPattern();
     //对每个路由都进行验证的中间件! @todo 如果是get方式的话,目标对象如何获取呢?当前用户是否拥有该资源?
     foreach ($this->middlewaresForEveryRoute as $validator) {
         $data = null;
         if (preg_match('|.*:.*|', $validator)) {
             //此处设置了可以带中间件参数
             list($validator, $data) = explode(':', $validator);
             $data = $dispatcher->getParam($data);
         }
         $validator = new $validator();
         if (!in_array($route->getName(), $validator->excludedRoutes) and !$validator->isValid($data)) {
             $url = $validator->getRedirectedUrl();
             //                    dd($url);
             $response->redirect($url, true);
             return false;
         }
     }
     //@todo 如果是get方式的如何过滤呢?应该如何设置才是正常的呢?例如get方式的search的过滤,单独处理?也许吧?
     if ($this->hasMatchedMiddleWares($pattern) and $request->isPost()) {
         $middleWares = $this->getMiddleWares($pattern);
         foreach ($middleWares as $validator) {
             $data = $request->getPost();
             //                dd($validator);
             if (preg_match('|.*:.*|', $validator)) {
                 list($validator, $data) = explode(':', $validator);
                 $data = $dispatcher->getParam($data);
             }
             $validator = new $validator();
             if (!$validator->isValid($data)) {
                 $url = $validator->getRedirectedUrl();
                 //                    dd($url);
                 $response->redirect($url, true);
                 return false;
             }
         }
     }
     return true;
 }
Exemplo n.º 7
0
 public function passThrouthMiddleWares(Request $request, Response $response, Dispatcher $dispatcher)
 {
     $route = $this->getMatchedRoute();
     if (null == $route) {
         $r = $this->getDI()->get('router');
         $r->handle($request->getURI());
         $route = $r->getMatchedRoute();
         //为什么搜索“装备”会出现找不到路由的问题?估计与字符处理有关系
         if (null == $route) {
             die('url地址无效,找不到对应的路由设置!');
         }
     }
     $pattern = $route->getPattern();
     //对每个路由都进行验证的中间件! @todo 如果是get方式的话,目标对象如何获取呢?当前用户是否拥有该资源?
     foreach ($this->middlewaresForEveryRoute as $validator) {
         $data = null;
         if (preg_match('|.*:.*|', $validator)) {
             //此处设置了可以带中间件参数
             list($validator, $data) = explode(':', $validator);
             $data = $dispatcher->getParam($data);
         }
         /** @var myValidation $validator */
         $validator = new $validator();
         if (!in_array($route->getName(), $validator->excludedRoutes) and !$validator->isValid($data)) {
             $url = $validator->getRedirectedUrl();
             //                    dd($url);
             $response->redirect($url, true);
             return false;
         }
     }
     //@todo 如果是get方式的如何过滤呢?应该如何设置才是正常的呢?例如get方式的search的过滤,单独处理?也许吧?
     if ($this->hasMatchedMiddleWares($pattern) and $request->isPost()) {
         $middleWares = $this->getMiddleWares($pattern);
         foreach ($middleWares as $validator) {
             $data = $request->getPost();
             //                dd($validator);
             if (preg_match('|[^:]+:[^:]+|', $validator)) {
                 list($validator, $data) = explode(':', $validator);
                 $data = $dispatcher->getParam($data);
             }
             if (preg_match('|.*Rules$|', $validator)) {
                 $rules = new $validator();
                 $validator = (new myValidation())->take($rules);
             } else {
                 $validator = new $validator();
             }
             if (!$validator->isValid($data)) {
                 $url = $validator->getRedirectedUrl();
                 //                    dd($url);
                 $response->redirect($url, true);
                 return false;
             }
         }
     }
     return true;
 }