parseRequest() public method

Parses the given request and returns the corresponding route and parameters.
public parseRequest ( UrlManager $manager, Request $request ) : array | boolean
$manager UrlManager the URL manager
$request Request the request component
return array | boolean the parsing result. The route and the parameters are returned as an array. If `false`, it means this rule cannot be used to parse this path info.
示例#1
1
 public function parseRequest($manager, $request)
 {
     $result = parent::parseRequest($manager, $request);
     if (isset($result[1]) && $result[1]['route']) {
         $route = $result[1]['route'];
         $page = Page::getDb()->cache(function () use($route) {
             return Page::find()->where(['route' => $route])->one();
         });
         if ($page) {
             return [$this->route, ['route' => $route]];
         }
     }
     return false;
 }
示例#2
0
 /**
  * @inheritdoc
  */
 public function parseRequest($manager, $request)
 {
     if ($result = parent::parseRequest($manager, $request)) {
         $this->redirectUrl = $this->redirectUrl ?: \Yii::$app->homeUrl;
         \Yii::$app->response->redirect($this->redirectUrl) && \Yii::$app->end();
     }
     return $result;
 }
示例#3
0
 /**
  * @inheritdoc
  */
 public function parseRequest($manager, $request)
 {
     list(, $params) = parent::parseRequest($manager, $request);
     foreach ($this->resolvers as $resolver) {
         if ($route = $resolver->resolve($params)) {
             return [$route, $params];
         }
     }
     return false;
 }
 /**
  * Redirects to destination, if current request matched.
  * @inheritdoc
  */
 public function parseRequest($manager, $request)
 {
     if ($parse = parent::parseRequest($manager, $request)) {
         list($route, $params) = $parse;
         $rule = array_merge([$route], $params);
         \Yii::$app->response->redirect($rule, $this->statusCode)->send();
         \Yii::$app->end();
     }
     return false;
 }
 /**
  * @inheritdoc
  */
 public function parseRequest($manager, $request)
 {
     // return the custom route
     $parts = explode("/", $request->pathInfo);
     if (!isset($parts[0]) || !Yii::$app->hasModule($parts[0]) || $parts[0] === 'cms') {
         return false;
     }
     // add trace info
     Yii::info('LUYA-CMS RouteBehaviorUrlRule is parsing the Request for path info \'' . $request->pathInfo . '\'', __METHOD__);
     return parent::parseRequest($manager, $request);
 }
示例#6
0
 /**
  * Parse request
  *
  * @param \yii\web\UrlManager $manager
  * @param \yii\web\Request $request
  *
  * @return array|bool
  */
 public function parseRequest($manager, $request)
 {
     $pathInfo = $request->getPathInfo();
     $url = preg_replace('#/$#', '', $pathInfo);
     $page = (new CmsModel())->findPage($url);
     if (!empty($page)) {
         $params['pageAlias'] = $url;
         $params['pageId'] = $page->id;
         return [$this->route, $params];
     }
     return parent::parseRequest($manager, $request);
 }
示例#7
0
 /**
  * @inheritdoc
  */
 public function parseRequest($manager, $request)
 {
     $pathInfo = $request->getPathInfo();
     $controller = $pathInfo ? Yii::$app->createController($pathInfo) : false;
     if (!$controller) {
         list(, $params) = parent::parseRequest($manager, $request);
         foreach ($this->resolvers as $resolver) {
             if ($route = $resolver->resolve($params)) {
                 return [$route, $params];
             }
         }
     }
     return false;
 }
示例#8
0
 public function parseRequest($manager, $request)
 {
     if ($parsedRequest = parent::parseRequest($manager, $request)) {
         list($route, $params) = $parsedRequest;
         if ($pi = $request->get($this->paramName)) {
             $pi = $this->urlDecode($pi);
             foreach ($pi as $key => &$value) {
                 $value = Json::decode($value);
             }
             $params = ArrayHelper::merge($params, $pi);
         }
         return [$route, $params];
     }
     return false;
 }
 /**
  * Parse request
  *
  * @param \yii\web\UrlManager $manager
  * @param \yii\web\Request $request
  *
  * @return boolean
  */
 public function parseRequest($manager, $request)
 {
     $pathInfo = $request->getPathInfo();
     // get path without '/' in end
     $url = preg_replace("#/\$#", "", $pathInfo);
     // find page by url in db
     $page = (new CmsModel())->findPage($url);
     // redirect to page
     if (!is_null($page)) {
         $params['pageAlias'] = $url;
         $params['pageId'] = $page->id;
         return [$this->route, $params];
     }
     return parent::parseRequest($manager, $request);
 }
示例#10
0
 /**
  * @inheritdoc
  */
 public function parseRequest($manager, $request)
 {
     if (!($result = parent::parseRequest($manager, $request))) {
         return false;
     }
     if ($this->enforceSeo) {
         Item::getCurrentPage($request->pathInfo);
         // sets seo meta tags
     }
     $manager->rules = array_filter($manager->rules, function ($rule) {
         return !$rule instanceof $this;
     });
     if (!($result2 = $manager->parseRequest($request))) {
         return $result;
     }
     return \Yii::$app->createController($result2[0]) ? $result2 : $result;
 }
示例#11
0
 /**
  * @param \yii\web\UrlManager $manager
  * @param \yii\web\Request $request
  * @return array|bool
  */
 public function parseRequest($manager, $request)
 {
     $res = parent::parseRequest($manager, $request);
     if (!$res) {
         return $res;
     }
     list($route, $params) = $res;
     if (isset($params[$this->languageParam])) {
         $languageSlug = $params[$this->languageParam];
         $language = I18N::getLanguageBySlug($languageSlug);
         if ($language) {
             Yii::$app->language = $language['locale'];
             Yii::trace("Change language by URL to: {$language['locale']}", __METHOD__);
         } elseif ($this->defaultLanguage !== null) {
             Yii::$app->language = $this->defaultLanguage;
         }
     }
     return $res;
 }
示例#12
0
 public function testParseRequest()
 {
     $manager = new UrlManager(['cache' => null]);
     $request = new Request(['hostInfo' => 'http://en.example.com']);
     $suites = $this->getTestsForParseRequest();
     foreach ($suites as $i => $suite) {
         list($name, $config, $tests) = $suite;
         $rule = new UrlRule($config);
         foreach ($tests as $j => $test) {
             $request->pathInfo = $test[0];
             $route = $test[1];
             $params = isset($test[2]) ? $test[2] : [];
             $result = $rule->parseRequest($manager, $request);
             if ($route === false) {
                 $this->assertFalse($result, "Test#{$i}-{$j}: {$name}");
             } else {
                 $this->assertEquals([$route, $params], $result, "Test#{$i}-{$j}: {$name}");
             }
         }
     }
 }
 public function parseRequest($manager, $request)
 {
     if (trim($request->getPathInfo(), '/') == $this->route && $request->get('id') && Categories::findOne($request->get('id'))) {
         header("Location:" . \Yii::$app->urlManager->createUrl([$this->route, 'id' => $request->get('id')]), true, 301);
         exit;
     }
     preg_match($this->pattern, \Yii::$app->request->getPathInfo(), $matches);
     $matches = $this->substitutePlaceholderNames($matches);
     $root = explode('/', trim($request->getPathInfo(), '/'));
     if (!isset($matches['parent'], $matches['alias']) && isset($root[1])) {
         $matches['alias'] = $root[1];
     }
     if (!isset($matches['alias'])) {
         return parent::parseRequest($manager, $request);
     }
     $model = Categories::findOne(['alias' => $matches['alias']]);
     if (!$model) {
         return parent::parseRequest($manager, $request);
     }
     if (isset($matches['parent']) && !Categories::findOne(['alias' => $matches['parent']])) {
         throw new NotFoundHttpException('Страница не найдена');
     }
     return [$this->route, ['id' => $model->id]];
 }