Php 如何在yii2中执行简单的搜索表单/功能?

Php 如何在yii2中执行简单的搜索表单/功能?,php,model-view-controller,yii2,Php,Model View Controller,Yii2,我想做一个搜索使用的形式,将搜索名称字段,品牌字段和类别字段 在这种情况下,我应该使用post还是get 如何/在何处创建查询搜索模型、模型或控制器 任何代码片段都很好 我认为: <?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'get', 'action' => Url::to(['productitem/search'])]); ?> <?= $form->

我想做一个搜索使用的形式,将搜索名称字段,品牌字段和类别字段

  • 在这种情况下,我应该使用post还是get
  • 如何/在何处创建查询搜索模型、模型或控制器
  • 任何代码片段都很好

    我认为:

    <?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'get', 'action' => Url::to(['productitem/search'])]); ?>
    
        <?= $form->field($productitem, 'name')->textInput(array('placeholder' => 'What are you looking for?'))->label(false) ?>
    
        <?= $form->field($productitem, 'brand_id')->dropDownList(
    
            ArrayHelper::map(ProductBrand::find()->all(),'id','name'),
            ['prompt'=>'Select Brand']
    
        )->label(false) ?>
    
        <?= $form->field($productitem, 'category_id')->dropDownList(
    
            ArrayHelper::map(ProductCategory::find()->all(),'id','name'),
            ['prompt'=>'Select Department']
    
        )->label(false) ?>
    
        <div class="form-group search-button">
            <button type="submit" class="btn btn-primary" name="login-button">Search <i class="fa fa-lg fa-arrow-circle-o-right"></i></button>
        </div>
    
        <?php ActiveForm::end(); ?>
    
    
    搜寻
    
    您的问题的答案:

  • 我使用get只是为了方便按下“后退”按钮。您根本没有更改数据库,因此GET应该提供足够的安全性/便利性
  • 搜索模型应在[model]搜索中。Yii2在普通模型的基础上有专门的搜索模型,为什么不保留它们呢?这正是他们在那里的目的
  • 没有代码片段,因此可以帮助您编写代码,但通常不会为您编写代码。但是,无论如何,可能会有人来给你填鸭式的代码,这样你就不会学到那么有趣的东西了