2011-12-21 2 views
0

우선 첫번째 처리와 유사한 표현, 간단한 경로 추가 :정규 표현식 충돌,

# The order is as-is in my environment 
Route::PATTERN('/^\/style\/(.*?)\/$/', 'style/[0].css'); 
Route::PATTERN('/^\/style\/(.*?)\/image-storage\/(.*?)\/$/', 'transformed/images/[1]'); 
Route::SIMPLE('/blog', 'parts/blog'); 

라우트가 나는 거라고는 아무것도 수행 할 수없는 경우 옵션들 (지금은 우선 순위에 따라 정렬되지 않지만

foreach(self::$routes as $route){ 
// lookup active route 
switch($route->type){ 
    case self::TYPE_SIMPLE: 
     if($route->lookup === $_SERVER['REQUEST_URI']) self::$active = $route; 
    break; 
    case self::TYPE_PATTERN: 
     if(preg_match_all($route->lookup, $_SERVER['REQUEST_URI'], $found)){ 
      // find all replaceable entries of route 
      preg_match_all('/((?<=\[)\d(?=\]))/', $route->location, $replace); 

      // remove first results 
      $lookup = array_shift($found); 
      $location = array_shift($replace); 

      // make the actual location out of both 
      foreach($replace[0] as $value) 
       if(isset($found[$value])) 
        $route->location = str_replace('[' . $value . ']', $found[$value][0], $route->location); 

      // this is active, cache it 
      self::$active = $route; 
     } 
    break; 
} 
} 
// I have left out the active route and routine parsing, because the problem is here at the 
// case self::TYPE_PATTERN part 

여기 것은 상기 제 2 패턴의 경로가 일치되는 것이 :)을 피하기 위해, 그리고 활성 경로가 결정되고, 루틴은 동일한 순서로 상기 추가 경로를 통한 주사 (foreach 루프) 후에 수행 첫번째. 미리보기를 사용하여이를 방지 할 수는 있지만 경로가 나중에 동적으로 중첩되어 예상 할 수없는 선행이 실패 할 수 있습니다.

요청 및 조회 (패턴)의 길이 비교에 대한 아이디어가 있지만 예측할 수없는 부분이기도합니다.

그럼,이 충돌을 어떻게 피할 수 있습니까?

답변

2

덜 일반적인 경로를 위에서 다른 경로 위로 이동하십시오. 그런 다음 일치하지 않는 경우에만 더 일반적인 시도가 시도됩니다.

Route::PATTERN('/^\/style\/(.*?)\/image-storage\/(.*?)\/$/', 'transformed/images/[1]'); 
Route::PATTERN('/^\/style\/(.*?)\/$/', 'style/[0].css'); 
+0

"우선 순위"는 같지만 그 유일한 방법은 무엇입니까? – jolt

+0

@Tom : 오, 당신이 다른 것을 의미한다고 생각했습니다. 이것은 내가 생각할 수있는 가장 단순하고, 쉽고, 가장 직접적인 방법입니다. 나는 그것이 일반적으로 어떻게 행해지는지 확실히 확신한다. – Cameron

+0

하지만 심하게 읽습니다 ... – jolt