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

标签 php yii2 custom-action

我有名为 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/

相关文章:

php - 隐藏 .show 后 JavaScript 出现问题

yii2 - 如何检测我是否处于 'console' 模式

css - Yii框架2.0添加媒体打印到css链接

windows-installer - 卸载的自定义操作类型 18 的序列位置应该是什么?

php - PHP中可以操作echo来写入文件吗

php - 如何使用 SSL/TLS 和/或消息级安全保护 RESTful php web 服务

mysql - Yii2 使用左连接将 SqlDataProvider 转换为 ActiveDataProvider

WIX 需要从 CustomAction 中的二进制文件中提取文件 (XML)

wix - 使用提升的权限但不作为 LocalSystem 调用自定义操作

php - 在不读取文件的情况下检查文件是否存在于不同的域中