0

I have a table country

CREATE TABLE `country` (
 `code` CHAR(2) NOT NULL PRIMARY KEY,
 `name` CHAR(52) NOT NULL,
 `population` INT(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `Country` VALUES ('AU','Australia',18886000);
INSERT INTO `Country` VALUES ('BR','Brazil',170115000);
INSERT INTO `Country` VALUES ('CA','Canada',1147000);

my model:

namespace app\models;

use yii\db\ActiveRecord;

class Country extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'country';
    }

    /**
     * @inheritdoc
     */
    public static function primaryKey()
    {
        return ['code'];
    }

    /**
     * Define rules for validation
     */
    public function rules()
    {
        return [
            [['code', 'name', 'population'], 'required']
        ];
    }
}

My controller:

<?php
   namespace app\controllers;
   use yii\rest\ActiveController;

   class CountryController extends ActiveController
   {
        public $modelClass = 'app\models\Country';
   }
 ?>

and this is my config in urlManager

'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            [
                'class' => 'yii\rest\UrlRule',
                'controller' => 'country',
                'tokens' => [
                    '{id}' => '<id:\\w+>'
                ]
            ]
        ],
    ],

the webservice works perfectly but I need return a html with this dates. For example, using Postman http://localhost/basic/web/countries/AU actually return

{
    "code": "AU",
    "name": "Australia",
    "population": 24016400
}

But I want that return html in the json content, for example:

{
  "content": "<html>
                <body>
                  <h1>AU</h1>
                  <p>Name: Australia</p>
                  <p>Population: 24016400</p>
                </body>
              </html>"
}

Is possible? Thank you

Gonzalo
  • 21
  • 3

0 Answers0