Routing Yii2忽略新控制器
我正在使用Yii2并尝试创建一个控制器,该控制器在子文件夹中呈现一个简单视图Routing Yii2忽略新控制器,routing,yii2,Routing,Yii2,我正在使用Yii2并尝试创建一个控制器,该控制器在子文件夹中呈现一个简单视图 我使用gii工具创建了一个基于简单mysql表的新模型 之后,我用CRUD功能创建了一个新的控制器 在这里,您可以看到我从gii>>积垢发生器输入的数据: 一切看起来都很好,但控制器将被完全忽略,因为当我在新的控制器代码中添加任何语法错误时,我没有从Yii2收到任何错误消息 我想这就是为什么控制器不能呈现我的视图的原因 所以我的具体问题是:我是否必须在Yii2中的何处注册一个新的控制器 我的观点是这样的: app
- 我使用gii工具创建了一个基于简单mysql表的新模型
- 之后,我用CRUD功能创建了一个新的控制器
<?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.');
}
}
oho-wow>>我发现有时Yii2在模型名中有两个大写字母的问题。这就是为什么我在没有下划线的情况下重命名了我的初始mysql表
然后,gii CRUD生成器将不会在ModelName中添加大写字母
然后我的控制器名是PaxartenController而不是PaxartenController
现在效果很好
希望这也能帮助其他有这个问题的人。你应该按大小访问只需添加破折号/pax-arten/index
。请看文件