简体   繁体   中英

Custom action in controller not working in yii2

I have controller called RegisterController and index view. If I submit the form, its showing the page not found (404) error . In the same if I change the method to actionCreate instead of actionAddBasic , working fine. Please help me to resolve the issue. Thanks.

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()
        ]);
    }

}

register/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>

I don't get the benefit of creating separate action in your case, but the error is here:

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

addBasic should be replaced with add-basic .

Action names transformed with dash and lowercase.

Also including index.php in url is redundant, this should be enough:

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

Or even this for the same controller:

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

Official documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM