我使用高级框架Yii2。我在前端有RegionController,在前端有Region模型。在视图中,我想用Select2调用公共功能Countrylist来显示所有国家/地区。但是当我尝试调用此函数时,异常是“找不到此页面”... 这是控制器:
\
这是我调用函数的视图代码:
<?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;
}
}
答案 0 :(得分:2)
尝试根据范围更改网址
$url = \yii\helpers\Url::to(['region\countrylist']);
或强>
$url = \yii\helpers\Url::to(['\region\countrylist']);