2016-06-28 42 views
1

我有一個yii rest api的問題。我將它配置爲遵循yii框架頁面上的教程工作,但在此之後,我意識到我的api可以工作,但不是我的PAGE的一些大部分,因爲它基於GET URL格式,而不是其餘api所需的PATH 。使用yii rest API的默認URL GET格式而不是PATH格式

所以在我的config/main.php我有以下設置

'urlManager' => array (
         'urlFormat' => 'path', 
         'rules' => array (
           'student/<id:\d+>/<title:.*?>' => 'student/view', 
           'students/<tag:.*?>' => 'student/index', 

           array (
             'apistudent/register', 
             'pattern' => 'api/<model:\w+>', 
             'verb' => 'POST' 
           ), 

           '<controller:\w+>/<action:\w+>' => '<controller>/<action>' 
         ) 
       ), 

我也有一個名爲控制器ApiStudentController一個叫actionRegister()方法。

因此,如前所述,API正常工作,但我的網頁沒有,因爲我將urlFormat設置爲'路徑'。

問題是...我怎樣才能使用其餘的api,但沒有PATH url格式,而是默認獲取url格式(index.php?r = apistudent/register)?

+0

嗨,你有任何解決方案? –

回答

0

我也在yii 1.x中遇到同樣的問題。我只需要使用舊的GET格式而不是PATH格式的API控制器(因爲我以PATH格式更改了我的網站URL)。最後,我得到它與腳本文件中的小黑客工作

$app = Yii::createWebApplication($env->configWeb); //store the app 
//Change the UrlFormat for urlManager to get if a get request is given instead of a path format one. 
if (isset($_GET['r'])) { 
    Yii::app()->urlManager->setUrlFormat('get'); 
} 
$app->run(); //run the app 

我不知道這是否解決您的問題。但這可以給你一個想法。快樂編碼!