Suche…


Beginnen Sie mit Rest API

Wir haben eine Tabelle mit Ländern, also erstellen wir ein Modell, das als länderspezifisches Modell bezeichnet wird

<?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',
        ];
    }
}

und ich erstelle einen rest Webservice, wir erstellen einen Controller für restapi und setzen die modelClass-Variable für unser Modell.

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

für restapi benötigen wir hübsche URLs und
Wir fügen diese Regel für eine hübsche URL hinzu

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

Danach können wir unsere Rest-API als Beispiel testen

http: // localhost / countrylist gibt uns eine Liste der Grafschaften.

Wie werden die Standardaktionen von rest api Yii2 überschrieben?

Als Beispiel möchten Sie die Paginierung in Ihrer Standardindexaktion deaktivieren und alle Ergebnisse im Index abrufen. Wie kannst du das machen? Es ist einfach. Sie sollten die Indexaktion in Ihrem Controller folgendermaßen überschreiben:

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;
}

Überschreiben Sie den Inhaltstyp für bestimmte Aktionen

Anwendungsfall: Nur eine Aktion, die einen einfachen (Text-) Inhalt unverändert zurückgeben soll

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

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

Vordefinierte Antwortformate sind:

  • FORMAT_HTML
  • FORMAT_XML
  • FORMAT_JSON
  • FORMAT_JSONP
  • FORMAT_RAW

Es gibt keinen MIME-Typ für text/plain aus der Box.

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
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow