yii2-netdisk/controllers/CountryController.php

135 lines
3.5 KiB
PHP
Raw Normal View History

2024-02-09 12:15:09 +08:00
<?php
namespace app\controllers;
use app\models\Country;
use app\models\CountrySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* CountryController implements the CRUD actions for Country model.
*/
class CountryController extends Controller
{
/**
* @inheritDoc
*/
public function behaviors()
{
return array_merge(
parent::behaviors(),
[
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
]
);
}
/**
* Lists all Country models.
*
* @return string
*/
public function actionIndex()
{
$searchModel = new CountrySearch();
$dataProvider = $searchModel->search($this->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Country model.
* @param string $code Code
* @return string
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($code)
{
return $this->render('view', [
'model' => $this->findModel($code),
]);
}
/**
* Creates a new Country model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return string|\yii\web\Response
*/
public function actionCreate()
{
$model = new Country();
if ($this->request->isPost) {
if ($model->load($this->request->post()) && $model->save()) {
return $this->redirect(['view', 'code' => $model->code]);
}
} else {
$model->loadDefaultValues();
}
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 $code Code
* @return string|\yii\web\Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($code)
{
$model = $this->findModel($code);
if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
return $this->redirect(['view', 'code' => $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 $code Code
* @return \yii\web\Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($code)
{
$this->findModel($code)->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 $code Code
* @return Country the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($code)
{
if (($model = Country::findOne(['code' => $code])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}