Question

I'm creating various 'my version' of web services. Basically, it's a yii controller with lots of actions.. each action is as follow

public function actionNameOfWebService()
{
   if(isset($_POST))
   {
      // do some processing, when I have a result... I do .. 
      print CJSON::encode('result.');
   }
   else
   {
      print CJSON::encode('only post methods allowed');
   }
}

Lots of those actions are in one particular controller. Everything's working fine..before I go to production, do I need to add a 'die;' statement after every print CJSON::encode statement.

Was it helpful?

Solution 4

You don't have to but when you add it, you'll be sure that nothing will be printed after JSON, which will break parsers attempts to read that data.

But if you know that there is nothing more, you can skip it.

OTHER TIPS

Since you are using Yii, you should simply use :

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

http://www.yiiframework.com/doc/api/1.1/CApplication#end-detail

No. You don't absolutely need die or exit. I would argue that using it is a bit of anti-pattern. It breaks encapsulation.

If you have some kind of output buffering set up the die might actually be bad. If you're proxying a request die will kill the whole process etc. There are more scenarios like this.

You definitely should use Yii::app()->end();This is because if you have any debugging output, or profiling output, this will also be included in the response unless you tell the app to finish. Your validation could break if you fail to include this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top