Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2.  
  3. namespace api\modules\v1\controllers;
  4.  
  5. use yii\rest\ActiveController;
  6.  
  7. /**
  8.  * Country Controller API
  9.  *
  10.  * @author Budi Irawan <deerawan@gmail.com>
  11.  */
  12. class CountryController extends ActiveController
  13. {
  14.     public $modelClass = 'api\modules\v1\models\Country';
  15.  
  16.     public function behaviors()
  17.     {
  18.         return [
  19.             'verbs' => [
  20.                 'class' => VerbFilter::className(),
  21.                 'actions' => [
  22.                     'delete' => ['POST'],
  23.                 ],
  24.             ],
  25.         ];
  26.     }
  27.  
  28.     /**
  29.      * Lists all Country models.
  30.      * @return mixed
  31.      */
  32.     public function actionIndex()
  33.     {
  34.         $searchModel = new CountrySearch();
  35.         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  36.  
  37.         return $this->render('index', [
  38.             'searchModel' => $searchModel,
  39.             'dataProvider' => $dataProvider,
  40.         ]);
  41.     }
  42.  
  43.     /**
  44.      * Displays a single Country model.
  45.      * @param string $id
  46.      * @return mixed
  47.      * @throws NotFoundHttpException if the model cannot be found
  48.      */
  49.     public function actionView($id)
  50.     {
  51.         return $this->render('view', [
  52.             'model' => $this->findModel($id),
  53.         ]);
  54.     }
  55.  
  56.     /**
  57.      * Creates a new Country model.
  58.      * If creation is successful, the browser will be redirected to the 'view' page.
  59.      * @return mixed
  60.      */
  61.     public function actionCreate()
  62.     {
  63.         $model = new Country();
  64.  
  65.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  66.             return $this->redirect(['view', 'id' => $model->code]);
  67.         }
  68.  
  69.         return $this->render('create', [
  70.             'model' => $model,
  71.         ]);
  72.     }
  73.  
  74.     /**
  75.      * Updates an existing Country model.
  76.      * If update is successful, the browser will be redirected to the 'view' page.
  77.      * @param string $id
  78.      * @return mixed
  79.      * @throws NotFoundHttpException if the model cannot be found
  80.      */
  81.     public function actionUpdate($id)
  82.     {
  83.         $model = $this->findModel($id);
  84.  
  85.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  86.             return $this->redirect(['view', 'id' => $model->code]);
  87.         }
  88.  
  89.         return $this->render('update', [
  90.             'model' => $model,
  91.         ]);
  92.     }
  93.  
  94.     /**
  95.      * Deletes an existing Country model.
  96.      * If deletion is successful, the browser will be redirected to the 'index' page.
  97.      * @param string $id
  98.      * @return mixed
  99.      * @throws NotFoundHttpException if the model cannot be found
  100.      */
  101.     public function actionDelete($id)
  102.     {
  103.         $this->findModel($id)->delete();
  104.  
  105.         return $this->redirect(['index']);
  106.     }
  107.  
  108.     /**
  109.      * Finds the Country model based on its primary key value.
  110.      * If the model is not found, a 404 HTTP exception will be thrown.
  111.      * @param string $id
  112.      * @return Country the loaded model
  113.      * @throws NotFoundHttpException if the model cannot be found
  114.      */
  115.     protected function findModel($id)
  116.     {
  117.         if (($model = Country::findOne($id)) !== null) {
  118.             return $model;
  119.         }
  120.  
  121.         throw new NotFoundHttpException('The requested page does not exist.');
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement