Oscar Reyes
Oscar Reyes

Reputation: 4352

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

I am testing Yii 2 advanced, i have created a view from CRUD and everything works fine, i can even get to see the index view, but when i create another CRUD which model is called CArea, it gets created but i cannot get to the view because it returns a 404 error response, the controller is generated with the name CAreaController and search is called CAreaSearch

I have been using Yii 1 and i have never encountered this error, if anyone would explain why it doesn't work, it would be appreciated

this is the generated controller

<?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.');
        }
    }
}

Upvotes: 2

Views: 1677

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

Well i think you should use his way for accessing your view

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

This because Yii2 routing notation need to format the camelCase in - (minus) separated string

Upvotes: 1

Related Questions