Yii2 implementation of three-level linkage between provinces, cities, and cities in China

Source: Internet
Author: User
This article describes how Yii2 achieves a three-level linkage between provinces, cities, and cities in China. I think this is a good example. I would like to share it with you and give you a reference. Let's take a look at it with Xiaobian. Yii2 implementation of three-level linkage between provinces, cities, and cities in China

Install

Add to your composer. json file

"chenkby/yii2-region": "dev-master"

Switch to the Project directory

Composer update;

3. configuration

1) add the following method to the Model of the region:

public static function getRegion($parentId=0){  $result = static::find()->where(['parent_id'=>$parentId])->asArray()->all();  return ArrayHelper::map($result, 'id', 'name');}

2) add the following actions to the controller:

public function actions(){  $actions=parent::actions();  $actions['get-region']=[    'class'=>\chenkby\region\RegionAction::className(),    'model'=>\app\models\Region::className()  ];  return $actions;}

3) add the enablePrettyUrl of urlManager to main. php in common/config to hide index. php.

"UrlManager" => [// indicates whether URL beautification is enabled for urlManager. in Yii1.1, it is called a URL in path format, and // rename and beautify in Yii2.0. // Disabled by default. However, it is generally enabled in actual use, especially in the product environment. "EnablePrettyUrl" => true, // whether to enable strict resolution. if strict resolution is enabled, the current request must match at least one routing rule. // otherwise, the request is considered invalid. // This option is only valid after enablePrettyUrl is enabled. "EnableStrictParsing" => false, // whether the script is displayed in the URL. Is a further supplement to the beautification function. "ShowScriptName" => false, // specifies a suffix after the URL, such as. html. Valid only when enablePrettyUrl is enabled. "Suffix" => "", "rules" => ["
 
  
/
  
   
"=>"
   
    
/View ","
    
     
/"=>"
     
      
/"],],
     
    
   
  
 

4. use

 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 ', 'proppt' => 'Select province '], 'City' => ['attribute' => 'city', 'items' => Region :: getRegion ($ model ['Province ']), 'options' => ['class' => 'form-control f Orm-control-inline ', 'proppt' => 'Select City'], 'District' => ['attribute' => 'District ', 'items '=> Region: getRegion ($ model ['city']), 'options' => ['class' => 'form-control form-control-inline', 'propt' => 'Select County/region']);?>

The above is all the content of this article. I hope it will help you learn and support PHP.

MoreYii2 implementation of three-level linkage between provinces, cities, and cities in ChinaFor more information, see PHP!

Related articles:

Using php to implement three-level linkage with database in urban areas

Js achieves provincial/municipal level-3 linkage menu effect

Sample code for implementing the ajax three-level linkage drop-down menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.