从示例中实现 Yii RESTful API - _sendResponse函数生成 500 错误


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

我正在尝试实现 http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api,但他们指定的_sendResponse函数遇到了一些问题。下面是我的函数,但首先,让我解释一下问题。

我有一个具有_sendResponse功能的abstract class BaseAPIController extends Controller。我有一个SomeObjectController extends BaseAPIController.在我的actionList函数中,我有以下行:

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

这将生成所需的输出。但是,状态代码为 500!

即使我这样做:

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

我仍然获得500级会员资格!

但是当我这样做时

$this->_sendResponse(200);

一切都很好,我得到了状态代码为 200 的预期空响应。

这简直让我发疯了!我需要做什么才能让它工作?

    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();
    }

多亏了一些受@acorncom启发的侦查,我看了一下错误日志:

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

当然,我有一个语法错误。而不是Yii::app()->end();我有Yii:app()->end().

如果有人能启发我为什么没有产生堆栈跟踪,我想知道。