Yii2忽略了新的控制器

时间:2019-03-03 12:05:07

标签: routing yii2

我正在使用Yii2并试图创建一个控制器,该控制器在子文件夹中呈现一个简单视图。

  • 我已经使用gii Tool创建了一个基于简单mysql表的新模型。

  • 之后,我使用CRUD功能创建了一个新的控制器

在这里您可以从gii >> CRUD生成器中看到我输入的数据:

enter image description here

一切看起来不错,但是控制器将被完全忽略,因为当我将任何语法错误添加到新的控制器代码中时,我没有从Yii2返回任何错误消息。

我认为这就是原因,为什么我的视图不会被控制器呈现。

所以我的具体问题是:我是否必须在Yii2的某个地方注册一个新的控制器?

我的观点是这样构成的:

app / views / paxarten / index.php app / views / paxarten / update.php

我的目标是通过此url结构访问它们

www.myApplication.com/paxarten/index

我的漂亮URL结构已经启用:)

感谢任何提示和帮助! 控制器

<?php

namespace app\controllers;

use Yii;
use app\models\PaxArten;
use app\models\PaxArtenSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * PaxArtenController implements the CRUD actions for PaxArten model.
 */
class PaxArtenController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all PaxArten models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new PaxArtenSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single PaxArten model.
     * @param integer $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 PaxArten model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new PaxArten();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing PaxArten model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $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->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing PaxArten model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $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 PaxArten model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return PaxArten the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = PaxArten::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

2 个答案:

答案 0 :(得分:-1)

oho wow >>我发现有时Yii2在Model名称中遇到2个大写字母的问题。这就是为什么我将我的初始mysql表重命名为没有下划线的原因。

然后,gii CRUD生成器将不会在模型名称中添加upperCase

然后我的控制器名称是PaxartenController而不是PaxArtenController

现在效果很好。

希望这对遇到此问题的其他人也有所帮助。

答案 1 :(得分:-2)

您应该按照SIZE所说的来访问http://www.myApplication.com/pax-arten/index