yiii高级:请求从生成的CRUD视图返回404错误


Yii2 Advanced: Request return 404 error from a generated CRUD view

我正在测试Yii 2 advanced,我已经从CRUD创建了一个视图,一切都很好,我甚至可以看到索引视图,但是当我创建另一个CRUD,该模型称为CArea,它被创建,但我无法获得视图,因为它返回404错误响应,控制器生成名称为CAreaController,搜索称为CAreaSearch

我一直在使用Yii 1,我从来没有遇到过这个错误,如果有人能解释为什么它不起作用,那将是感激的

这是生成的控制器

<?php
namespace backend'controllers;
use Yii;
use common'models'CArea;
use common'models'search'CAreaSearch;
use yii'web'Controller;
use yii'web'NotFoundHttpException;
use yii'filters'VerbFilter;
/**
 * CAreaController implements the CRUD actions for CArea model.
 */
class CAreaController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Lists all CArea models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new CAreaSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single CArea model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    /**
     * Creates a new CArea model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CArea();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    /**
     * Updates an existing CArea model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
    /**
     * Deletes an existing CArea model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }
    /**
     * Finds the CArea model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CArea the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CArea::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}

我认为你应该使用他的方式来访问你的视图

  localhost/backend/web/index.php?r=c-area

这是因为Yii2路由符号需要将camelCase格式化为-(减去)分隔的字符串