2012-10-25 4 views
1

URL에 API 버전을 관리하기 위해 parseUrl을 구현하려고하는 Yii와 Im의 새로운 기능입니다. /api/1.0/ ...을 컨트롤러 apiV1.0으로 리디렉션하고 싶습니다.Yii Api 버전 urlManager/parseUrl

Main.php (urlManager 규칙) :

... 
array('class' => 'application.components.ApiVersion'), 
... 

가 ApiVersion.php :

<?php 
    class ApiVersion extends CBaseUrlRule 
    { 
    public $connectionID = 'db'; 

    public function createUrl($manager,$route,$params,$ampersand) 
    { 
     if ($route==='car/index') 
     { 
      if (isset($params['manufacturer'], $params['model'])) 
       return $params['manufacturer'] . '/' . $params['model']; 
      else if (isset($params['manufacturer'])) 
       return $params['manufacturer']; 
     } 
     return false; // this rule does not apply 
    } 

    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo) 
    { 
     if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches)) 
      $url = $match[1] . $match[2]; // BUILD YOUR URL (controllerName/actionName) 
      $this->redirect(array($url)); 
     } 
     return false; 
    } 
} 

임 매우 작동 방법과 손실, 누군가가 나를 도울 수 있을까? 경로가 "API/num_of_version/your_model/an_id"그래서

귀하의 URL이이 상태에있는이 일치하는 경우, 당신은 GET을 설정해야합니다 같은이 정규식 체크하면 함수에서

답변

1

값 :

$_GET['apiversion'] = $matches[2]; 
$_GET['model'] = $matches[4]; 
$_GET['id'] = $matches[6]; 

그리고 버전에 따라 당신이 좋은 컨트롤러 반환 : 당신이 이해할 필요가

return "apiV" . $_GET['apiversion'] . "/view"; 

일부 링크 :

How preg_match works (what is the structure of $matches)

편집 : 당신은 몇 가지 문제가 지금 나를 주시기 바랍니다이 특정 지점이 있다면, 난이 시점에 좀 더 내 대답을 ellaborate하려고합니다.

+0

안녕하세요. 여전히 작동하지 않는데, 나는 createUrl 함수에서 무엇을해야합니까? –

+0

첫 번째 글을 편집했습니다. –

+0

편집시 createUrl 및 parseUrl이 모두 수행해야하는 작업에 맞지 않습니다! U는 문서에서 가져온 것뿐입니다! – darkheir