yii\web\UrlRule::init PHP Method

init() public method

Initializes this rule.
public init ( )
    public function init()
    {
        if ($this->pattern === null) {
            throw new InvalidConfigException('UrlRule::pattern must be set.');
        }
        if ($this->route === null) {
            throw new InvalidConfigException('UrlRule::route must be set.');
        }
        if (is_array($this->normalizer)) {
            $normalizerConfig = array_merge(['class' => UrlNormalizer::className()], $this->normalizer);
            $this->normalizer = Yii::createObject($normalizerConfig);
        }
        if ($this->normalizer !== null && $this->normalizer !== false && !$this->normalizer instanceof UrlNormalizer) {
            throw new InvalidConfigException('Invalid config for UrlRule::normalizer.');
        }
        if ($this->verb !== null) {
            if (is_array($this->verb)) {
                foreach ($this->verb as $i => $verb) {
                    $this->verb[$i] = strtoupper($verb);
                }
            } else {
                $this->verb = [strtoupper($this->verb)];
            }
        }
        if ($this->name === null) {
            $this->name = $this->pattern;
        }
        $this->pattern = trim($this->pattern, '/');
        $this->route = trim($this->route, '/');
        if ($this->host !== null) {
            $this->host = rtrim($this->host, '/');
            $this->pattern = rtrim($this->host . '/' . $this->pattern, '/');
        } elseif ($this->pattern === '') {
            $this->_template = '';
            $this->pattern = '#^$#u';
            return;
        } elseif (($pos = strpos($this->pattern, '://')) !== false) {
            if (($pos2 = strpos($this->pattern, '/', $pos + 3)) !== false) {
                $this->host = substr($this->pattern, 0, $pos2);
            } else {
                $this->host = $this->pattern;
            }
        } else {
            $this->pattern = '/' . $this->pattern . '/';
        }
        if (strpos($this->route, '<') !== false && preg_match_all('/<([\\w._-]+)>/', $this->route, $matches)) {
            foreach ($matches[1] as $name) {
                $this->_routeParams[$name] = "<{$name}>";
            }
        }
        $tr = ['.' => '\\.', '*' => '\\*', '$' => '\\$', '[' => '\\[', ']' => '\\]', '(' => '\\(', ')' => '\\)'];
        $tr2 = [];
        if (preg_match_all('/<([\\w._-]+):?([^>]+)?>/', $this->pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $name = $match[1][0];
                $pattern = isset($match[2][0]) ? $match[2][0] : '[^\\/]+';
                $placeholder = 'a' . hash('crc32b', $name);
                // placeholder must begin with a letter
                $this->placeholders[$placeholder] = $name;
                if (array_key_exists($name, $this->defaults)) {
                    $length = strlen($match[0][0]);
                    $offset = $match[0][1];
                    if ($offset > 1 && $this->pattern[$offset - 1] === '/' && (!isset($this->pattern[$offset + $length]) || $this->pattern[$offset + $length] === '/')) {
                        $tr["/<{$name}>"] = "(/(?P<{$placeholder}>{$pattern}))?";
                    } else {
                        $tr["<{$name}>"] = "(?P<{$placeholder}>{$pattern})?";
                    }
                } else {
                    $tr["<{$name}>"] = "(?P<{$placeholder}>{$pattern})";
                }
                if (isset($this->_routeParams[$name])) {
                    $tr2["<{$name}>"] = "(?P<{$placeholder}>{$pattern})";
                } else {
                    $this->_paramRules[$name] = $pattern === '[^\\/]+' ? '' : "#^{$pattern}\$#u";
                }
            }
        }
        $this->_template = preg_replace('/<([\\w._-]+):?([^>]+)?>/', '<$1>', $this->pattern);
        $this->pattern = '#^' . trim(strtr($this->_template, $tr), '/') . '$#u';
        if (!empty($this->_routeParams)) {
            $this->_routeRule = '#^' . strtr($this->route, $tr2) . '$#u';
        }
    }

Usage Example

Example #1
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->pattern = $this->dashboardId . '/<' . ConfigComponent::FIRST_SEGMENT . '>/<' . ConfigComponent::SECOND_SEGMENT . '>/<' . ConfigComponent::THIRD_SEGMENT . '>';
     $this->route = $this->dashboardId . '/default/index';
     $this->defaults = [ConfigComponent::THIRD_SEGMENT => 0];
     parent::init();
 }
All Usage Examples Of yii\web\UrlRule::init