Yii2 Advanced:从生成的CRUD视图请求返回404错误

时间:2015-10-10 23:54:03

标签: php yii yii2

我正在测试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.');
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为你应该用他的方式来访问你的观点

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

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