【问题标题】:action create not working Yii2 frontend操作创建不工作 Yii2 前端
【发布时间】:2017-11-07 02:11:52
【问题描述】:

我想在我的视图前端执行 action create,站点控制器中 actionCreate 的代码是正确的,但它用这个 url “http://localhost/advanced/frontend/web/index.php?Subsidize%5Bname%5D............ " 知道在“补贴”表中创建新项目后我想重定向到 view.php

在siteController.php中创建动作

public function actionView($id) {
    $model = Subsidize::findOne($id);
    if ($model === null) {
        throw new NotFoundHttpException;
    }

    return $this->render('view', [
                'model' => $model,
    ]);
}

public function actionCreate() {
    $model = new Subsidize();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {

         return $this->redirect(['view', 'id' => $model->subsidize_id]);


    } else {

        return $this->render('create', [
                    'model' => $model,
        ]);
    }
}

视图“create.php”中提交按钮的代码

        <?= Html::submitButton($model->isNewRecord ? 'إرسال ' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

另外,siteController 中的函数行为

 public function behaviors() {
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['create'],
            'rules' => [
                [
                    'actions' => ['login', 'error'],
                    'allow' => true,
                ],
                [
                    'actions' => ['create', 'view'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    //see captcha and error added here, this fixes the issue
                    'actions' => ['support', 'test', 'delete', 'update', 'create', 'view'],
                    'allow' => true,
                    'roles' => ['?', '@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'index' => ['get'],
                'view' => ['get'],
                'create' => ['get', 'post'],
                'update' => ['get', 'put', 'post'],
                'delete' => ['post', 'delete'],
            ],
        ],
    ];
}

我的观点代码:create.php

<?php use yii\helpers\Html; use yii\widgets\ActiveForm; $model = new app\models\Subsidize; ?> <section class="support">
<div class="container">       
    <form class="dialog-form row">
        <?php $form = ActiveForm::begin() ?>
        <div class="col-md-12">
            <div class="form-group">             


                <?php echo $form->field($model, 'name', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('name', ['placeholder' => "الإسم الكريم"])->label(false); ?>

            </div><!--End Form-group-->
        </div><!-- col -->
        <div class="col-md-12">
            <div class="form-group">
                 <?php echo $form->field($model, 'montant', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('montant', ['placeholder' => "المبلغ "])->label(false); ?>
            </div><!--End Form-group-->
        </div><!-- col -->

         <div class="col-md-12">
            <div class="form-group">
                 <?php echo $form->field($model, 'date', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('date', ['placeholder' => "تاريخ التذكير "])->label(false); ?>
            </div><!--End Form-group-->
        </div><!-- col -->

        <div class="col-md-12">
            <div class="form-group">
                 <?php echo $form->field($model, 'phone', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('phone', ['placeholder' => "رقم الجوال"])->label(false); ?>
            </div><!--End Form-group-->
        </div><!-- col -->



        <div class="col-md-12">
            <div class="form-group">

                    <?php echo $form->field($model, 'remarks', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput(['rows' =>6])->input('remarks', ['placeholder' => "ملاحظات"])->label(false); ?>
            </div><!-- form-group -->
        </div><!-- col -->
        <div class="col-md-12">
            <div class="form-group" style="text-align:center "> 


    <?= Html::submitButton($model->isNewRecord ? 'إرسال ' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>



            </div><!-- form-group -->
        </div><!-- col -->
         <?php ActiveForm::end(); ?>
    </form><!--End-->
</div><!-- container -->

【问题讨论】:

    标签: php yii2 action


    【解决方案1】:

    您是否忘记指定 HTML 表单的 method 属性?

    <form method="POST" ...>
    

    看起来表单只是作为 GET 请求提交

    【讨论】:

    • 你的意思是在我的活动表单中
    • 啊,好的,默认应该是POST
    • 我忘了告诉你我在我的站点控制器中添加了这个函数 public function beforeAction($action) { $this->enableCsrfValidation = false;返回父级::beforeAction($action); }
    • 代码似乎没问题。也许重定向被缓存。尝试在禁用缓存的情况下重复。例如,在 Network 标签上使用带有 Disable cache 复选框的 Chrome 开发者控制台。
    • 我删除了对我的动词行为的创建操作,但同样的错误
    【解决方案2】:

    对不起我之前的英语。您可以编辑您的帖子并添加完整的ActiveForm::begin() 代码。对于替代修复,您可以尝试删除对动词行为的创建操作,如下所示:

    'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'index' => ['get'],
                'view' => ['get'], 
                'update' => ['get', 'put', 'post'],
                'delete' => ['post', 'delete'],
            ],
        ],
    

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多