using yii rest api with default URL GET format rather than PATH format

299 Views Asked by At

I have a problem with a yii rest api. I configured it to work following the tutorial on the yii framework page, but after that i realised that my api works BUT NOT some big PORTIONS of my PAGE since it is based on the GET URL format rather than PATH which is required by the rest api.

So in my config/main.php i have the following setting

'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>' 
                        ) 
                ),

I also have a controller named ApiStudentController with a method called actionRegister().

So as already stated the api works normally but my page does not since i set the urlFormat to be 'path'.

The question is... how can i use the rest api but without the PATH url format and rather the default get url format (index.php?r=apistudent/register)?

1

There are 1 best solutions below

0
On

I too faced the same problem in yii 1.x. I just need my API controller alone in old GET format rather than in PATH format (as i changed my websites URLs in PATH format). Finally i got it worked with a small hack in script file

$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

I dont know whether this solves your problem. But this can give you an idea. Happy Coding!