《PHP學(xué)習(xí):Yii2實(shí)現(xiàn)中國(guó)省市區(qū)三級(jí)聯(lián)動(dòng)實(shí)例》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Yii2實(shí)現(xiàn)中國(guó)省市區(qū)三級(jí)聯(lián)動(dòng)實(shí)例,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:YII框架
1、獲取源碼:yii2-region_jb51.rarPHP實(shí)例
2、安裝PHP實(shí)例
添加到你的composer.json文件PHP實(shí)例
"chenkby/yii2-region": "dev-master"
切換到項(xiàng)目目錄PHP實(shí)例
composer update;
PHP實(shí)例
3、配置PHP實(shí)例
1) 在地區(qū)的Model中添加以下方法PHP實(shí)例
public static function getRegion($parentId=0) { $result = static::find()->where(['parent_id'=>$parentId])->asArray()->all(); return ArrayHelper::map($result, 'id', 'name'); }
2) 在controller中添加以下actionPHP實(shí)例
public function actions() { $actions=parent::actions(); $actions['get-region']=[ 'class'=>\chenkby\region\RegionAction::className(), 'model'=>\app\models\Region::className() ]; return $actions; }
3) 在common/config里的main.php中添加 urlManager的enablePrettyUrl的,即隱藏index.phpPHP實(shí)例
"urlManager" => [ //用于表明urlManager是否啟用URL美化功能,在Yii1.1中稱(chēng)為path格式URL, // Yii2.0中改稱(chēng)美化. // 默認(rèn)不啟用.但實(shí)際使用中,特別是產(chǎn)品環(huán)境,一般都會(huì)啟用. "enablePrettyUrl" => true, // 是否啟用嚴(yán)格解析,如啟用嚴(yán)格解析,要求當(dāng)前請(qǐng)求應(yīng)至少匹配1個(gè)路由規(guī)則, // 否則認(rèn)為是無(wú)效路由. // 這個(gè)選項(xiàng)僅在 enablePrettyUrl 啟用后才有效. "enableStrictParsing" => false, // 是否在URL中顯示入口腳本.是對(duì)美化功能的進(jìn)一步補(bǔ)充. "showScriptName" => false, // 指定續(xù)接在URL后面的一個(gè)后綴,如 .html 之類(lèi)的.僅在 enablePrettyUrl 啟用時(shí)有效. "suffix" => "", "rules" => [ "<controller:\w+>/<id:\d+>"=>"<controller>/view", "<controller:\w+>/<action:\w+>"=>"<controller>/<action>" ], ],
4、使用PHP實(shí)例
<?= $form->field($model, 'district')->widget(\chenkby\region\Region::className(),[ 'model'=>$model, 'url'=> \yii\helpers\Url::toRoute(['get-region']), 'province'=>[ 'attribute'=>'province', 'items'=>Region::getRegion(), 'options'=>['class'=>'form-control form-control-inline','prompt'=>'選擇省份'] ], 'city'=>[ 'attribute'=>'city', 'items'=>Region::getRegion($model['province']), 'options'=>['class'=>'form-control form-control-inline','prompt'=>'選擇城市'] ], 'district'=>[ 'attribute'=>'district', 'items'=>Region::getRegion($model['city']), 'options'=>['class'=>'form-control form-control-inline','prompt'=>'選擇縣/區(qū)'] ] ]); ?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.PHP實(shí)例
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/1834.html