Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace api\modules\v1\controllers;
- use yii\rest\ActiveController;
- /**
- * Country Controller API
- *
- * @author Budi Irawan <deerawan@gmail.com>
- */
- class CountryController extends ActiveController
- {
- public $modelClass = 'api\modules\v1\models\Country';
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ];
- }
- /**
- * Lists all Country models.
- * @return mixed
- */
- public function actionIndex()
- {
- $searchModel = new CountrySearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Country model.
- * @param string $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new Country model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $model = new Country();
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->code]);
- }
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- /**
- * Updates an existing Country model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param string $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->code]);
- }
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- /**
- * Deletes an existing Country model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param string $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- /**
- * Finds the Country model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param string $id
- * @return Country the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Country::findOne($id)) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement