To define your own URL parsing and creation logic you can extend from this class and add it to [[UrlManager::rules]] like this: ~~~ 'rules' => [ ['class' => 'MyUrlRule', 'pattern' => '...', 'route' => 'site/index', ...], ... ] ~~~
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Object, implements yii\web\UrlRuleInterface
コード例 #1
1
ファイル: PageUrlRule.php プロジェクト: NullRefExcep/yii2-cms
 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
 public function actionIndex()
 {
     echo "*************************************************";
     $rule = new UrlRule(['pattern' => 'debug/<controller:[\\w\\-]+>/<action:[\\w\\-]+>/<id:\\d+>', 'route' => 'debug/<controller>/<action>', 'defaults' => ['id' => 10, 'action' => 'method']]);
     $rule->createUrl(Yii::$app->urlManager, 'debug/test/index', []);
     //        $urlManager = Yii::$app->urlManager;
     //        var_dump($urlManager->rules);
 }
コード例 #3
0
 /**
  * @param \yii\web\UrlManager $manager
  * @param string $route
  * @param array $params
  * @return bool|string
  */
 public function createUrl($manager, $route, $params)
 {
     if (!isset($params[$this->languageParam]) || !$params[$this->languageParam]) {
         $params[$this->languageParam] = I18N::getCurrentLanguageSlug();
     }
     return parent::createUrl($manager, $route, $params);
 }
コード例 #4
0
 /**
  * @inheritdoc
  */
 public function createUrl($manager, $route, $params)
 {
     if (!isset($params[$this->languageParam])) {
         $params[$this->languageParam] = $this->getLanguage();
     }
     return parent::createUrl($manager, $route, $params);
 }
コード例 #5
0
ファイル: RedirectUrlRule.php プロジェクト: bariew/yii2-tools
 /**
  * @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;
 }
コード例 #6
0
ファイル: UrlRule.php プロジェクト: stronglab/yii2-dashboard
 /**
  * Replace dashboard routes in url
  * 
  * @param object $manager
  * @param string $route
  * @param array $params
  * @return bool
  */
 public function createUrl($manager, $route, $params)
 {
     $customRoute = $this->prepareRoute($route);
     if (Yii::$app->getModule($this->dashboardId)->config->isDashboardRoute($customRoute) && Yii::$app->getModule($this->dashboardId)->config->isDashboardCall()) {
         $extRoute = Yii::$app->getModule($this->dashboardId)->config->getRoute($customRoute);
         return parent::createUrl($manager, $this->dashboardId . '/default/index', array_merge($params, $this->paramzRoute($extRoute)));
     }
     return parent::createUrl($manager, $route, $params);
 }
コード例 #7
0
 /**
  * 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;
 }
コード例 #8
0
 /**
  * @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);
 }
コード例 #9
0
ファイル: GroupUrlRule.php プロジェクト: xyxdasnjss/imshop
 /**
  * @inheritdoc
  */
 public function createUrl($manager, $route, $params)
 {
     $defaultRoute = $this->route;
     foreach ($this->resolvers as $resolver) {
         $this->route = $resolver->getRoute();
         if ($url = parent::createUrl($manager, $route, $params)) {
             return $url;
         }
     }
     $this->route = $defaultRoute;
     return parent::createUrl($manager, $route, $params);
 }
コード例 #10
0
ファイル: PageUrlRule.php プロジェクト: yii2mod/yii2-cms
 /**
  * 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);
 }
コード例 #11
0
ファイル: UrlRuleTest.php プロジェクト: aivavic/yii2
 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}");
             }
         }
     }
 }
コード例 #12
0
 /**
  * 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);
 }
コード例 #13
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;
 }
コード例 #14
0
ファイル: WebUrlRule.php プロジェクト: weblement/yii2
 public function createUrl($manager, $route, $params)
 {
     if (isset($params[$this->paramName]) && !is_array($params[$this->paramName])) {
         $params[$this->paramName] = $this->urlDecode($params[$this->paramName]);
         foreach ($params[$this->paramName] as $key => &$value) {
             $value = Json::decode($value);
         }
     }
     foreach ($params as $key => $param) {
         if (in_array($key, $this->autoEncodeParams) && !is_null($param)) {
             $params[$this->paramName][$key] = $param;
             ArrayHelper::remove($params, $key);
         }
     }
     if (isset($params[$this->paramName]) && is_array($params[$this->paramName])) {
         foreach ($params[$this->paramName] as $key => &$value) {
             $value = Json::encode($value);
         }
         $params[$this->paramName] = $this->urlEncode($params[$this->paramName]);
     }
     return parent::createUrl($manager, $route, $params);
 }
コード例 #15
0
ファイル: UrlRule.php プロジェクト: ivan-chkv/yii2-mozayka
 public function createUrl($manager, $route, $params)
 {
     $request = Yii::$app->getRequest();
     if (!array_key_exists('modelId', $params)) {
         $modelId = $request->getQueryParam('modelId');
         if ($modelId) {
             $params['modelId'] = $modelId;
         }
     }
     if (strncmp($route, 'mozayka/crud/', 13) == 0) {
         $controller = Yii::$app->controller;
         if ($controller->id == 'crud') {
             if ($controller->action->id == 'list') {
                 if ($route != 'mozayka/crud/list') {
                     $params['uniqId'] = uniqid();
                 }
             } else {
                 $uniqId = $request->getQueryParam('uniqId');
                 if ($uniqId) {
                     $session = Yii::$app->getSession();
                     if (!$session->has($uniqId)) {
                         $session->set($uniqId, $request->getReferrer());
                     }
                     if ($route != 'mozayka/crud/list') {
                         $params['uniqId'] = $uniqId;
                     } else {
                         $referrer = $session->get($uniqId);
                         if ($referrer) {
                             return $referrer;
                         }
                     }
                 }
             }
         }
     }
     return parent::createUrl($manager, $route, $params);
 }
コード例 #16
0
 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]];
 }
コード例 #17
0
 public function init()
 {
     $this->addLanguagePrefixToPattern();
     parent::init();
 }
コード例 #18
0
 /**
  * @inheritdoc
  */
 public function createUrl($manager, $route, $params)
 {
     $params = $this->checkLanguageParams($params);
     return parent::createUrl($manager, $route, $params);
 }