1

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();
    }
pnuts
  • 58,317
  • 11
  • 87
  • 139
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
  • Where do you have the code for _getStatusCodeMessage specified? In your BaseAPIController? Are you using accessFilters that are causing problems? – acorncom Apr 17 '12 at 13:05
  • @acorncom it is specified in the `BaseAPIController`, yes. That base controller is an abstract class. – tacos_tacos_tacos Apr 17 '12 at 17:51
  • I guess my question is, if you print out $status within _sendResponse (say, immediately after it comes into the function), has it been changed to $status = 500 or is that some glitch with _getStatusCodeMessage() ? – acorncom Apr 17 '12 at 21:46
  • Sample output: the header has status code 500, but my output is `HTTP/1.1 200 OK{"id":"2","name":"Artemis Big Breed Fresh Mix"}`, which is what I would expect. (`echo $status` after headers sent out but before the rest of the function) – tacos_tacos_tacos Apr 18 '12 at 02:45

1 Answers1

2

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.

tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
  • 1
    PHP Fatal errors are not caught within Yii. I've requested this multiple times (as it's doable), but the core team has reasons against implementing such support. – Jon L. Aug 07 '12 at 06:25