我无法使用 Yii 2.0 在控制器中获取 $_POST 值

I cannot grab a $_POST value in controller using Yii 2.0

使用 Yii 2.0,我试图从我的视图中获取我的控制器中的一些 $_POST 值,但无法做到这一点。我将向您展示我的代码并在下面向您讲解。

Models/CaseSearch.php

 <?php

 namespace app\models;

 use Yii;
 use yii\base\Model;
 use yii\data\ActiveDataProvider;
 use app\models\Cases;

 /**
 * CaseSearch represents the model behind the search form about `app\models\Cases`.
 */

class CaseSearch extends Cases
{

public $category;
public $subcategory;
public $childcategory;
public $newcategory;
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['case_id', 'year'], 'integer'],
        [['name', 'judgement_date', 'neutral_citation', 'all_ER', 'building_law_R', 'const_law_R', 'const_law_J', 'CILL', 'adj_LR'], 'safe'],
    ];
}

/**
 * @inheritdoc
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = Cases::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    $query->andFilterWhere([

        'case_id' => $this->case_id,
        'judgement_date' => $this->judgement_date,
        'year' => $this->year,
    ]);

    $query->andFilterWhere(['like', 'name', $this->name])
        ->andFilterWhere(['like', 'neutral_citation', $this->neutral_citation])
        ->andFilterWhere(['like', 'all_ER', $this->all_ER])
        ->andFilterWhere(['like', 'building_law_R', $this->building_law_R])
        ->andFilterWhere(['like', 'const_law_R', $this->const_law_R])
        ->andFilterWhere(['like', 'const_law_J', $this->const_law_J])
        ->andFilterWhere(['like', 'CILL', $this->CILL])
        ->andFilterWhere(['like', 'adj_LR', $this->adj_LR]);







    return $dataProvider;
}

public function searchByCategory($category){
    $query = Cases::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);



    if (!$this->validate()) {
        // uncomment the following line if you do not want to any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    $query->andFilterWhere([

        'category_id' => $category

    ]);

    return $dataProvider;
}   
}

好的,现在是我的观点:

  <?php


    $form = ActiveForm::begin();

    echo $form->field($searchModel, 'category')
                ->dropDownList(
                    ArrayHelper::map($allCategory, 'id', 'name'),
                    [
                        'onchange'=>'getSubcategory()',
                    ]
    );

    //To stop errors, if first category not chosen make subcategory and empty drop down.
    $subcategory = array(
        "empty" => ""

    );

    echo $form->field($searchModel, 'subcategory')
                ->dropDownList(
                    ArrayHelper::map($subcategory, 'id', 'name'),
                    [
                       'onchange'=>'getChildcategory()',
                    ]
    );
    //To stop errors, if second category not chosen make childcategory and empty drop down.
    $childcategory = array(
        "empty" => ""
    );
    echo $form->field($searchModel, 'childcategory')
                ->dropDownList(
                    ArrayHelper::map($childcategory, 'id', 'name'),
                    [
                       //'onchange'=>'getChildCategory()',
                        'onchange'=>'submitNow()',
                    ]
    );



    echo '<div class="form-group">';
        echo Html::submitButton('Submit', ['class' => 'btn btn-primary']);
    echo '</div>';
    ActiveForm::end();

好的,所以当我单击提交按钮时,我想在我的控制器中捕获该值,以便我可以使用它来更改 gridview 中给出的结果。

当我检查下拉列表中的元素时,名称很奇怪,所以我不确定这是否有所不同。例如,子类别的名称实际上是:CaseSearch[subcategory]

现在我的控制器:

public function actionIndex()
{
//
// This is for the first render of index
//
    $model = new Cases;
    $searchModel = new CaseSearch();  
    $allCategory = Category::find()->all();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);       
    //
    // This is for when i click the submit button on the view. I want it to submit and then grab the subcategory in variable $subtest. I make $subtest = 1 so that when i first render the page it doesn't throw an error and when submitted it should change to the post value
    // 

    $subtest = 1;
    if($searchModel->load(Yii::$app->request->post())){
        $subtest = $_POST['subcategory'];

    }

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'allCategory' => $allCategory,
        'model' => $model,  
        'subtest' => $subtest
    ]);
}

然而,当我尝试 print_r() 变量 $subtest 在我看来,我得到错误:

  Undefined index: CaseSearch[subcategory]

及其对应的行:

  $subtest = $_POST['CaseSearch[subcategory]'];

在我的控制器中。

谁能告诉我,我不知道为什么?

不要在控制器中直接使用 $_GET$_POST 有一些抽象层可以过滤该输入。

$get = $request->get(); 
// equivalent to: $get = $_GET;

$post = $request->post(); 
// equivalent to: $post = $_POST;

您应该使用 类 和方法来获取您的参数。

http://www.yiiframework.com/doc-2.0/guide-runtime-requests.html

我认为您的应用程序设计有待改进。阅读 Yii 指南。 但是我认为问题的解决方案是:

  $subtest = $_POST['CaseSearch']['subcategory];