Search code examples
phpyiiyii-inheritance

Implementing Yii RESTful API from example - _sendResponse function generating 500 error


I am trying to implement http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api and having some trouble with the _sendResponse function they specify. Below is my function, but first, let me explain the problem.

I have an abstract class BaseAPIController extends Controller that has the _sendResponse function. I have a SomeObjectController extends BaseAPIController. In my actionList function I have the line:

$this->_sendResponse(200, CJSON::encode($rows), 'application/json');

which generates the desired output. HOWEVER, the status code is 500!

Even when I do:

$this->_sendResponse(200, ' ', 'application/json');

I still get status 500!

But when I do

$this->_sendResponse(200);

all is well and I get the expected empty response with status code 200.

This is driving me crazy! What do I need to do to get this to work?

    protected function _sendResponse($status = 200, $body = '', $content_type='text/html')
    {
            $status_header = 'HTTP/1.1 ' . $status . ' ' . $this->_getStatusCodeMessage($status);

            header($status_header);

            header('Content-Type: ' . $content_type);

            if ($body != '')
            {
                    echo $body;
            }

            else
            {
                    $message = '';

                    switch($status)
                    {
                            case 401:
                                    $message = 'Not authorized.';
                                    break;
                            case 404:
                                    $message = 'Not found.';
                                    break;
                            case 500:
                                    $message = 'Internal server error.';
                                    break;
                            case 501:
                                    $message = 'Method not implemented.';
                                    break;
                            default:
                                    break;
                    }

                    $signature = ($_SERVER['SERVER_SIGNATURE'] == '') ?
                            $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] :
                            $_SERVER['SERVER_SIGNATURE'];

                    // Needs to be completed with templates.

                    $body = '
                            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                            "http://www.w3.org/TR/html4/strict.dtd">
                            <html>
                            <head>
                                    <meta http="Content-Type" content="text/html"; charset=iso-8859-1">
                                    <title>' . $status . ' ' . $this->_getStatusCodeMessage($status) . '</title>
                            </head>
                            <body>
                                    <h1>' . $this->_getStatusCodeMessage($status) . '</h1>
                                    <p>' . $message . '</p>
                                    <hr />
                            </body>
                            </html>
                            ';
                    echo $body;
            }

            Yii:app()->end();
    }

Solution

  • Thanks to some sleuthing inspired by @acorncom, I took a look at the error log:

    PHP Fatal error: Call to undefined function app() in /Library/WebServer/Documents/iK9/iK9/iK9/protected/modules/api/controllers/BaseAPIController.php on line 68

    and of course, I had a syntax error. Instead of Yii::app()->end(); I had Yii:app()->end().

    If someone would enlighten me as to why that did not produce a stack trace I would like to know.