yii\helpers\BaseJson::encode PHP Method

encode() public static method

The method enhances json_encode() by supporting JavaScript expressions. In particular, the method will not encode a JavaScript expression that is represented in terms of a [[JsExpression]] object.
public static encode ( mixed $value, integer $options = 320 ) : string
$value mixed the data to be encoded.
$options integer the encoding options. For more details please refer to . Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`.
return string the encoding result.
    public static function encode($value, $options = 320)
    {
        $expressions = [];
        $value = static::processData($value, $expressions, uniqid('', true));
        set_error_handler(function () {
            static::handleJsonError(JSON_ERROR_SYNTAX);
        }, E_WARNING);
        $json = json_encode($value, $options);
        restore_error_handler();
        static::handleJsonError(json_last_error());
        return $expressions === [] ? $json : strtr($json, $expressions);
    }

Usage Example

 public function actionConsult()
 {
     // $searchModel = new LocaleSearch();
     // $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     $model = $this->findModel($_POST['id']);
     echo BaseJson::encode($model);
 }
All Usage Examples Of yii\helpers\BaseJson::encode