首页 > 解决方案 > Call function in view Yii2 with Select2

问题描述

I use advanced framework Yii2. I have RegionController in frontend and Region model in frontend. In view i want to call a public function Countrylist with Select2 to show all countries. But when i try to call this function, exception is "this page is not found" ... here is controller:

    <?php

namespace frontend\controllers;

use Yii;
use frontend\models\Region;
use frontend\models\Country;
use frontend\models\RegionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
 * RegionController implements the CRUD actions for Region model.
 */
class RegionController extends Controller
{

    public function actionRegionlist($q = null, $id = null) {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!is_null($q)) {
            $query = new Query;
            $query->select('id, name AS text')
                ->from('region')
                ->where(['like', 'name', $q])
                ->andWhere(['country_id' => $_GET['country']])
                ->limit(20);
            $command = $query->createCommand();
            $data = $command->queryAll();
            $out['results'] = array_values($data);
        }
        elseif ($id > 0) {
            $out['results'][] = ['id' => $id, 'text' => Region::find($id)->name];
        }
        return $out;
    }

    public function actionCountrylist($q = null, $id = null) {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            $out = ['results' => ['id' => '', 'text' => '']];
            if (!is_null($q)) {
                $data = array();
                $arr = ArrayHelper::map(Country::find()->select('country.id, country.name')->filterWhere(['like', 'country.name', $q])->limit(10)->all(), 'id', 'name');
                ksort($arr);
                if ($arr) {
                    $k = 0;
                    foreach ($arr as $id => $value) {
                        $data[$k]['id'] = $id;
                        $data[$k]['text'] = $value;
                        $k++;
                    }
                }
                $out['results'] = array_values($data);
            }
            return $out;
    }
}

here is a view code where i call the function:

     <div class="col-md-4">
        <div class="form-group">
         <?php
           $cityName = empty($model->city_id) ? '' : City::findOne($model->city_id)->name;
           $url = \yii\helpers\Url::to(['..\..\region\countrylist']);
         ?>
         <?=
         $form->field($model, 'city_id')->widget(Select2::classname(), [
                      'initValueText' => $cityName,
                      'theme' => 'bootstrap',
                      'options' => [
                      'placeholder' => Yii::t('app', 'app.choose'),
                      'class' => 'form-control select2'
         ],
         'pluginOptions' => [
         'allowClear' => true,
         'minimumInputLength' => 3,
         'ajax' => [
                    'url' => $url,
                    'dataType' => 'json',
                    'data' => new JsExpression('function(params) { 
                              return {q:params.term, region:$("#profile-region_id").val()}; }')
                              ],
                              'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                              'templateResult' => new JsExpression('function(city) { return city.text; }'),
                              'templateSelection' => new JsExpression('function (city) { return city.text; }'),
                           ],
                      ])
                 ?>
    </div>
</div>

标签: yii2jquery-select2yii2-advanced-app

解决方案


尝试根据范围更改 URL

$url = \yii\helpers\Url::to(['region\countrylist']);

或者

$url = \yii\helpers\Url::to(['\region\countrylist']);

推荐阅读