php 如何在 yii 中获得 json 格式(应用程序/json)的响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2824805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get response as json format(application/json) in yii?
提问by tq0fqeu
How to get response as json format(application/json) in yii?
如何在 yii 中获得 json 格式(应用程序/json)的响应?
回答by marcovtwout
For Yii 1:
对于 Yii 1:
Create this function in your (base) Controller:
在您的(基本)控制器中创建此函数:
/**
* Return data to browser as JSON and end application.
* @param array $data
*/
protected function renderJSON($data)
{
header('Content-type: application/json');
echo CJSON::encode($data);
foreach (Yii::app()->log->routes as $route) {
if($route instanceof CWebLogRoute) {
$route->enabled = false; // disable any weblogroutes
}
}
Yii::app()->end();
}
Then simply call at the end of your action:
然后只需在您的操作结束时调用:
$this->renderJSON($yourData);
For Yii 2:
对于 Yii 2:
Yii 2 has this functionality built-in, use the following code at the end of your controller action:
Yii 2 内置了此功能,请在控制器操作的末尾使用以下代码:
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $data;
回答by Neil McGuigan
$this->layout=false;
header('Content-type: application/json');
echo CJavaScript::jsonEncode($arr);
Yii::app()->end();
回答by Sergey Onishchenko
For Yii2 inside a controller:
对于控制器内的 Yii2:
public function actionSomeAjax() {
$returnData = ['someData' => 'I am data', 'someAnotherData' => 'I am another data'];
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$response->data = $returnData;
return $response;
}
回答by tq0fqeu
$this->layout=false;
header('Content-type: application/json');
echo json_encode($arr);
Yii::app()->end();
回答by Andrey Mischenko
class JsonController extends CController {
protected $jsonData;
protected function beforeAction($action) {
ob_clean(); // clear output buffer to avoid rendering anything else
header('Content-type: application/json'); // set content type header as json
return parent::beforeAction($action);
}
protected function afterAction($action) {
parent::afterAction($action);
exit(json_encode($this->jsonData)); // exit with rendering json data
}
}
class ApiController extends JsonController {
public function actionIndex() {
$this->jsonData = array('test');
}
}
回答by Truongnq
In the controller action that you want to render JSONdata, e.g: actionJson()
在要呈现JSON数据的控制器操作中,例如:actionJson()
public function actionJson(){
$this->layout=false;
header('Content-type: application/json');
echo CJSON::encode($data);
Yii::app()->end(); // equal to die() or exit() function
}
See more Yii API
查看更多Yii API
回答by Developer
one more simple way by using
一种更简单的方法是使用
echo CJSON::encode($result);
example code:
示例代码:
public function actionSearch(){
if (Yii::app()->request->isAjaxRequest && isset($_POST['term'])) {
$models = Model::model()->searchNames($_POST['term']);
$result = array();
foreach($models as $m){
$result[] = array(
'name' => $m->name,
'id' => $m->id,
);
}
echo CJSON::encode($result);
}
}
cheers :)
欢呼:)
回答by Mihailoff
Yii::app()->end()
I think this solution is not the best way to end application flow, because it uses PHP's exit()function, witch means immediate exit from execution flow. Yes, there is Yii's onEndRequesthandler, and PHP's register_shutdown_functionbut it still remains too fatalistic.
我认为这个解决方案不是结束应用程序流程的最佳方式,因为它使用了 PHP 的exit()功能,witch 意味着立即退出执行流程。是的,有 Yii 的onEndRequest处理程序和 PHP的处理程序,register_shutdown_function但它仍然太宿命论了。
For me the better way is this
对我来说更好的方法是这个
public function run($actionID)
{
try
{
return parent::run($actionID);
}
catch(FinishOutputException $e)
{
return;
}
}
public function actionHello()
{
$this->layout=false;
header('Content-type: application/json');
echo CJavaScript::jsonEncode($arr);
throw new FinishOutputException;
}
So, the application flow continues to execute even after.
因此,即使在此之后,应用程序流仍会继续执行。

