使用 Select2 在视图 Yii2 中调用函数

Call function in view Yii2 with Select2

我使用的是高级框架Yii2。我在前端有 RegionController,在前端有 Region 模型。在视图中,我想用 Select2 调用 public 函数 Countrylist 来显示所有国家。但是当我尝试调用这个函数时,异常是 "this page is not found" ... 这是控制器:

    <?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;
    }
}

这是我调用函数的视图代码:

     <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>

根据范围尝试更改Url

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

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