Zoeken…


Begin met rust api

We hebben een tabel met landen, dus we maken een model dat countrylist-model wordt genoemd

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "countrylist".
 *
 * @property integer $id
 * @property string $iso
 * @property string $name
 * @property string $nicename
 * @property string $iso3
 * @property integer $numcode
 * @property integer $phonecode
 */
class Countrylist extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'countrylist';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['iso', 'name', 'nicename', 'phonecode'], 'required'],
            [['numcode', 'phonecode'], 'integer'],
            [['iso'], 'string', 'max' => 2],
            [['name', 'nicename'], 'string', 'max' => 80],
            [['iso3'], 'string', 'max' => 3]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'iso' => 'Iso',
            'name' => 'Name',
            'nicename' => 'Nicename',
            'iso3' => 'Iso3',
            'numcode' => 'Numcode',
            'phonecode' => 'Phonecode',
        ];
    }
}

en daarvoor creëer ik rest-webservice, we creëren een controller voor restapi en stellen modelClass-variabele in voor ons model.

 <?php
    namespace app\controllers;
    use yii\rest\ActiveController;
    use Yii;
    class CountrylistController extends ActiveController
    {
      public $modelClass='app\models\Countrylist';
    }
?>

voor het gebruik van restapi hebben we mooie urls en nodig
we voegen deze regel toe voor een mooie url

'urlManager' => [
   'class' => 'yii\web\UrlManager',
   'enablePrettyUrl' => true,
   'showScriptName' => false,
   'rules' => [
       ['class'=>'yii\rest\UrlRule','controller'=>'countrylist']
       ],
    ],

daarna hebben we toegang tot onze rust api als een voorbeeld te testen

http: // localhost / countrylist geeft ons een lijst met provincies.

Hoe standaardacties van rest api Yii2 te overschrijven

Als voorbeeld wilt u paginering in uw standaardindexactie uitschakelen en alle resultaten in index krijgen. Hoe kan je dat doen? Het is makkelijk. U moet de indexactie in uw controller als volgt overschrijven:

public function actions() {
    $actions = parent::actions();
    unset($actions['index']);
    return $actions;
}

public function actionIndex() {
    $activeData = new ActiveDataProvider([
        'query' => \common\models\Yourmodel::find(),
        'pagination' => false
    ]);
    return $activeData;
}

Inhoudstype overschrijven voor specifieke actie

Gebruikscasus: slechts één actie die een gewone (tekst) inhoud als zodanig moet retourneren:

public function actionAsXML()
{
    $this->layout = false;
    Yii::$app->response->format = Response::FORMAT_XML;

    return ['aaa' => [1, 2, 3, 4]];;
}

Vooraf gedefinieerde antwoordformaten zijn:

  • FORMAT_HTML
  • FORMAT_XML
  • FORMAT_JSON
  • FORMAT_JSONP
  • FORMAT_RAW

Er is geen mime-type voor text/plain uit de doos, gebruik dit in plaats daarvan:

public function actionPlainText()
{
    $this->layout = false;
    Yii::$app->response->format = Response::FORMAT_RAW;
    Yii::$app->response->headers->add('Content-Type', 'text/plain');

    return $this->render('plain-text'); // outputs template as plain text
}


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow