gpt4 book ai didi

php - Controller 中的自定义操作在 yii2 中不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 20:38:42 24 4
gpt4 key购买 nike

我有名为 RegisterController 的 Controller 和索引 View 。如果我提交表单,它会显示找不到页面 (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>

最佳答案

在你的案例中我没有得到创建单独操作的好处,但错误在这里:

'action' => ['index.php/register/addBasic'],

addBasic 应替换为 add-basic

Action 名称用破折号和小写字母转换。

同时在 url 中包含 index.php 是多余的,这应该足够了:

'action' => ['register/add-basic'],

甚至对于同一个 Controller 也是如此:

'action' => ['add-basic'],

官方文档:

关于php - Controller 中的自定义操作在 yii2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28930007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com