1

I have created new controller in the project with gii but I am getting Not Found (#404) error when I try to access it.please help me why i get this error? here is the controller:

<?php

namespace backend\controllers\phone;

use Yii;
use common\models\phone\UssdCode;
use backend\models\phone\UssdCodeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UssdCodeController implements the CRUD actions for UssdCode model.
 */
class UssdCodeController extends Controller
{

    public function getViewPath()
    {
//        return \Yii::getAlias('@backend/views/phone/ussdcode');
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

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

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

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

    /**
     * Updates an existing UssdCode 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->UssdCodeId]);
        }

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

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

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

and here is the model:

<?php

namespace common\models\phone;

use Yii;

/**
 * This is the model class for table "tbl_ussd_code".
 *
 * @property int $UssdCodeId
 * @property int $CategoryIdRef
 * @property int $SubCategoryIdRef
 * @property string $CodeDescription
 * @property string $UssdCode
 * @property int $Status
 */
class UssdCode extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tbl_ussd_code';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['CategoryIdRef', 'SubCategoryIdRef', 'CodeDescription', 'UssdCode'], 'required'],
            [['CategoryIdRef', 'SubCategoryIdRef', 'Status'], 'integer'],
            
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'UssdCodeId' => 'Ussd Code ID',
            
        ];
    }
}

and here is the index:

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel \backend\models\phone\UssdCodeSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Ussd Codes';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="ussd-code-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Create Ussd Code', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'UssdCodeId',
            'CategoryIdRef',
            'SubCategoryIdRef',
            'CodeDescription',
            'UssdCode',
            //'Status',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>


</div>

Access url is: http://localhost/test/backend/ussdcode/index

why I get Not Found (#404)??

2 Answers2

0

You have to be careful with the naming of the controllers and ....

For example:

ussdcode becomes app\controllers\UssdcodeController;
ussd-code becomes app\controllers\UssdCodeController;

*This means that the view folder is named as ussd-code and the URl address is called as ussd-code.

Controller Class Naming

Controller class names can be derived from controller IDs according to the following procedure:

  • Turn the first letter in each word separated by hyphens into upper case. Note that if the controller ID contains slashes, this rule only applies to the part after the last slash in the ID.

  • Remove hyphens and replace any forward slashes with backward slashes.

  • Append the suffix Controller.

  • Prepend the controller namespace.

user206
  • 1,085
  • 1
  • 10
  • 15
0

It seems to be issue related to namespaces. In the controller UssdCodeController you are user following namespace statement:

namespace backend\controllers\phone;

For controllers, it should not have sub-folders. Move your controller files directly under backend\controllers folder, and change the namespace like:

namespace backend\controllers;

And you are good to go.

soodssr
  • 41
  • 1
  • 6