Heim >Backend-Entwicklung >PHP-Tutorial >YII框架接收不到post参数?

YII框架接收不到post参数?

WBOY
WBOYOriginal
2016-07-06 13:51:532292Durchsuche

先上代码 错误肯定就在这两个里边 但是不知道怎么解决 附网址:http://182.61.37.188/?r=login/entry

  1. LoginController

<code><?php /**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:28
 */

namespace app\controllers;
use Yii;
use yii\base\Controller;
use app\models\Userdb;
use app\models\EntryForm;

class LoginController extends Controller{
    public function actionEntry(){

        $model = new EntryForm;
        if ($model->load(Yii::$app->request->post())) {

            print_r($model);
            print_r(Yii::$app->request->post('userName'));
            $info = new  Userdb();
            $info->userName = $model->username;
            $info->password = $model->password;
            $info->save();

            return $this->render('showinfo', ['model' => $model]);
        }else{
            return $this->render('entry',['model' => $model]);
        }

    }
}</code>

2.entry.php

<code><?php /**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:37
 */

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => "{label}\n<div class='\"col-lg-3\"'>{input}</div>\n<div class='\"col-lg-8\"'>{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>

= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

= $form->field($model, 'password')->passwordInput() ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            = Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
        </div>
    </div>

<?php ActiveForm::end(); ?></code>

回复内容:

先上代码 错误肯定就在这两个里边 但是不知道怎么解决 附网址:http://182.61.37.188/?r=login/entry

  1. LoginController

<code><?php /**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:28
 */

namespace app\controllers;
use Yii;
use yii\base\Controller;
use app\models\Userdb;
use app\models\EntryForm;

class LoginController extends Controller{
    public function actionEntry(){

        $model = new EntryForm;
        if ($model->load(Yii::$app->request->post())) {

            print_r($model);
            print_r(Yii::$app->request->post('userName'));
            $info = new  Userdb();
            $info->userName = $model->username;
            $info->password = $model->password;
            $info->save();

            return $this->render('showinfo', ['model' => $model]);
        }else{
            return $this->render('entry',['model' => $model]);
        }

    }
}</code>

2.entry.php

<code><?php /**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:37
 */

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => "{label}\n<div class='\"col-lg-3\"'>{input}</div>\n<div class='\"col-lg-8\"'>{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>

= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

= $form->field($model, 'password')->passwordInput() ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            = Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
        </div>
    </div>

<?php ActiveForm::end(); ?></code>

做个调试看看,你先别用框架自带的Yii::$app->request->post()获取,直接在控制器方法程序入口var_dump($_POST)看看有没有值

通过$model->load(Yii::$app->request->post())就已经获取到了EntryForm的实例$model;打印$model->username试试

你可以尝试这种方法:

<code>$model->load(Yii::$app->request->post(), '' );</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn