Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use Yii;
  6.  
  7. /**
  8.  * This is the model class for table "products".
  9.  *
  10.  * @property int $id
  11.  * @property string $name
  12.  * @property int $supplier_id
  13.  */
  14. class Product extends \yii\db\ActiveRecord
  15. {
  16.     /**
  17.      * {@inheritdoc}
  18.      */
  19.     public static function tableName()
  20.     {
  21.         return 'products';
  22.     }
  23.  
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     public function rules()
  28.     {
  29.         return [
  30.             [['supplier_id'], 'integer'],
  31.             [['name'], 'string', 'max' => 255],
  32.         ];
  33.     }
  34.  
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function attributeLabels()
  39.     {
  40.         return [
  41.             'id' => 'ID',
  42.             'name' => 'Name',
  43.             'supplier_id' => 'Supplier ID',
  44.         ];
  45.     }
  46. }
  47.  
  48. Controller - ProductController
  49.  
  50. <?php
  51.  
  52. namespace app\Controllers;
  53.  
  54. use app\models\Product;
  55. use yii\data\ActiveDataProvider;
  56. use yii\rest\ActiveController;
  57.  
  58. class ProductController extends ActiveController
  59. {
  60.     public $modelClass = 'app\models\Product';
  61.     public $serializer = [
  62.         'class' => 'yii\rest\Serializer',
  63.         'collectionEnvelope' => 'items',
  64.     ];
  65.  
  66.     public function actionIndex()
  67.     {
  68.         var_dump('here');
  69.     }
  70. }
  71.  
  72. and routes in url manager
  73.  
  74. ...
  75.  'urlManager' => [
  76.             'enablePrettyUrl' => true,
  77.             'showScriptName' => false,
  78.             'enableStrictParsing' => true,
  79.             'rules' => [
  80.                 ['class' => 'yii\rest\UrlRule', 'controller' => 'product'],
  81.             ],
  82.         ],
  83. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement