DefaultController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace common\modules\urlrule\backend\controllers;
  3. use common\components\Controller;
  4. use Yii;
  5. use yii\data\ActiveDataProvider;
  6. use yii\web\NotFoundHttpException;
  7. class DefaultController extends Controller
  8. {
  9. public function actions()
  10. {
  11. return [
  12. "delete" => 'backend\actions\Delete'
  13. ];
  14. }
  15. public function actionIndex()
  16. {
  17. $query = \common\modules\urlrule\models\UrlRule::find();
  18. $dataProvider = new ActiveDataProvider(['query' => $query]);
  19. $model = new \common\modules\urlrule\models\UrlRule();
  20. return $this->render('index', [
  21. 'dataProvider' => $dataProvider,
  22. 'model' => $model,
  23. ]);
  24. }
  25. public function actionCreate()
  26. {
  27. $model = new \common\modules\urlrule\models\UrlRule();
  28. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  29. return $this->redirect(['index']);
  30. } else {
  31. Yii::$app->session->setFlash('error', current($model->getFirstErrors()));
  32. return $this->redirect(['index']);
  33. }
  34. }
  35. public function actionUpdate($id)
  36. {
  37. $model = $this->findModel($id);
  38. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  39. return $this->redirect(['index']);
  40. } else {
  41. return $this->render('update', [
  42. 'model' => $model,
  43. ]);
  44. }
  45. }
  46. /**
  47. * Finds the Page model based on its primary key value.
  48. * If the model is not found, a 404 HTTP exception will be thrown.
  49. *
  50. * @param int $id
  51. *
  52. * @return \common\modules\urlrule\models\UrlRule the loaded model
  53. *
  54. * @throws NotFoundHttpException if the model cannot be found
  55. */
  56. public function findModel($id)
  57. {
  58. if (($model = \common\modules\urlrule\models\UrlRule::findOne($id)) !== null) {
  59. return $model;
  60. } else {
  61. throw new NotFoundHttpException('The requested page does not exist.');
  62. }
  63. }
  64. }