0

コハナでルーターがどのように機能するかを理解しようとしています。

私はメソッドコンパイルに行き、困難に直面しました...

この行がここにある理由:

 $expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);

メソッドのコンパイル:

public static function compile($uri, array $regex = NULL)
    {
        // The URI should be considered literal except for keys and optional parts
        // Escape everything preg_quote would escape except for : ( ) < >
        $expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);

        if (strpos($expression, '(') !== FALSE)
        {
            // Make optional parts of the URI non-capturing and optional
            $expression = str_replace(array('(', ')'), array('(?:', ')?'), $expression);
        }

        // Insert default regex for keys
        $expression = str_replace(array('<', '>'), array('(?P<', '>'.Route::REGEX_SEGMENT.')'), $expression);

        if ($regex)
        {
            $search = $replace = array();
            foreach ($regex as $key => $value)
            {
                $search[]  = "<$key>".Route::REGEX_SEGMENT;
                $replace[] = "<$key>$value";
            }

            // Replace the default regex with the user-specified regex
            $expression = str_replace($search, $replace, $expression);
        }

        return '#^'.$expression.'$#uD';
    }



const REGEX_ESCAPE  = '[.\\+*?[^\\]${}=!|]';

理解するのに役立つ別の記事を表示できますか?

4

1 に答える 1

2
// What must be escaped in the route regex
const REGEX_ESCAPE  = '[.\\+*?[^\\]${}=!|]';

// The URI should be considered literal except for keys and optional parts
// Escape everything preg_quote would escape except for : ( ) < >
$expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);

コードのこの部分は、すべての文字 (丸括弧と角括弧を除く) がエスケープされることを意味します。特定のルートで疑問符やドットを検出するのに役立ちます。

\\\\$0

バックスラッシュを使用するには、正規表現でそれを複製する必要があります。

この preg_replace を使用した結果の例:

テスト => テスト

テスト/ => テスト/

//テスト/ => //テスト/

//テスト/!=> //テスト/#!

//test/!#$ => //test/!#\$

//test/!#$%^&*aaa()bbb => //test/!#\$%\^&*aaa()bbb

于 2013-10-28T10:23:55.597 に答える