我有一个名为RegisterController和索引视图的控制器。如果我提交表单,则会显示页面未找到(404)错误。同样如果我将方法改为 actionCreate而不是actionAddBasic ,工作正常。请帮我解决这个问题。感谢。
RegisterController.php
<?php
namespace app\controllers;
use app\models\Address;
use yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\BasicInfo;
use app\models\User;
/**
* Class RegisterController
* To Handle Registration Related Requests.
*/
class RegisterController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['addBasic'],
'rules' => [
[
'actions' => ['addBasic'],
'allow' => true,
'roles' => ['?'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'addBasic' => ['post'],
],
],
];
}
/**
* To Load Registration Page.
*/
public function actionIndex()
{
$user = new User();
$basicInfo = new BasicInfo();
$basicInfo->gender = 1;
$fileUpload = new FileUpload();
$addressModel = new Address();
return $this->render('index', [
'userModel' => $user,
'basicInfoModel' => $basicInfo,
'fileUploadModel' => $fileUpload,
'addressModel' => $addressModel
]);
}
public function actionAddBasic()
{
yii::trace('Inside AddBasic');
return $this->render('index', [
'userModel' => new User(),
'basicInfoModel' => new BasicInfo(),
'fileUploadModel' => new FileUpload(),
'addressModel' => new Address()
]);
}
}
寄存器/ index.php的:
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use app\models\MaritalStatus;
use app\models\ProfileFor;
?>
<div class="form">
<?php $form = ActiveForm::begin(['id' => 'form-signup-1', 'action' => ['index.php/register/addBasic']]); ?>
<div class="row">
<?= $form->field($basicInfoModel, 'name')->textInput(['maxlength' => 25, 'placeholder' => 'Name']) ?>
</div>
<div class="row">
<?= $form->field($userModel, 'email')->input('email', ['maxlength' => 30, 'placeholder' => 'Email']) ?>
</div>
<div class="row">
<?= $form->field($userModel, 'mno')->input('text', ['maxlength' => 10, 'placeholder' => 'Mobile Number']) ?>
</div>
<div class="row" style="float: right;">
<?= Html::submitButton('Create', ['class' => 'btn btn-success']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
答案 0 :(得分:3)
我没有从你的案例中创建单独的行动的好处,但错误在这里:
'action' => ['index.php/register/addBasic'],
addBasic
应替换为add-basic
。
用短划线和小写字母转换的动作名称。
在url中包含index.php
也是多余的,这应该足够了:
'action' => ['register/add-basic'],
甚至对于同一个控制器:
'action' => ['add-basic'],
官方文档: