Provides various official and user-released code examples and code reference. You are welcome to exchange and learn about the recent e-commerce project. You have read a lot of posts to implement one-stop and multiple cities, I found some flaws, and finally thought of using an empty controller to access the city substation.
Now I will post the code and share it with you, and solicit better optimization suggestions.
For example, zz.58.com will not be introduced. Anyone who uses it can trust me.
Take 58 as an example.
Common/Conf/config. php
Return array (
'App _ SUB_DOMAIN_DEPLOY '=> 1, // enable subdomain Configuration
'App _ SUB_DOMAIN_RULES '=> array (
'Www '=> 'home', // use www.58.com to access
'Bizz' => 'admin', // use biz.58.com to access
'M' => 'wap ', // use m.58.com to access
),
);Home/Conf/config. php
Return array (
/* Enable the routing function */
'Url _ ROUTER_ON '=> true,
'Url _ ROUTE_RULES '=> array (
// Static Routing is preferred.
'Cities '=> 'city/change', // you can use www.58.com/cities to select a City.
'Login' => 'public/login', // use www.58.com/login.pdf For login
'Reg '=> 'public/reg', // use www.58.com/reg.pdf for registration
// Start Dynamic Routing
'Buy/: id \ d' => 'order/buy', // use www.58.com/buy/18.pdf to buy products with the id of 18.
': City/deal/: id \ d' => 'deal/Index', // use www.58.com/zz/deal/18.pdf to open the 18 commodity page of the Zhengzhou page.
': City/: category' => 'category/list', // you can use www.58.com/zz/meishi.pdf to classify Zhengzhou food.
),
);Home/Controller/CommonController. class. php
Namespace Home \ Controller;
Use Think \ Controller;
Class CommonController extends Controller {
/**
* Background controller Initialization
*/
Protected function _ initialize (){
Header ('content-type: text/html; charset = UTF-8 ');
/* Read the configuration in the database */
$ Config = S ('db _ CONFIG_DATA ');
If (! $ Config ){
$ Config = D ('admin/config')-> getConfig ();
S ('db _ CONFIG_DATA ', $ config );
}
C ($ config); // Add Configuration
If (C ('web _ SITE_STATUS ') = 'n '){
$ This-> assign ("message", 'website is being maintained and cannot be accessed temporarily ');
$ This-> error ();
}
/* Obtain UID */
If (session ('? Uid') = false ){
Session ('uid', '0 ');
}
}
}Home/Controller/IndexController. class. php
Namespace Home \ Controller;
Use Think \ Controller;
Class IndexController extends CommonController {
Public function _ before_index (){
/* Determine the cookie */
If (isset ($ _ COOKIE ['cityid']) {
$ City = $ _ COOKIE ['cityurl'];
Redirect ('/'. $ city );
} Else {
Redirect ('cities ');
}
}
Public function index (){
Echo 'if you see this page, it indicates that the program has an error! ';
}
}Home/Controller/EmptyController. class. php
Namespace Home \ Controller;
Use Think \ Controller;
Class EmptyController extends CommonController {
Public function index (){
// Determine the city to perform operations based on the current controller name
$ CityName = strtolower (CONTROLLER_NAME );
If (isset ($ _ COOKIE ['cityurl']) and $ cityName ==$ _ COOKIE ['cityurl']) {
$ This-> city ($ cityName );
} Else {
$ Cityinfo = M ('categoryregion')-> field ('Id, title, url')-> where (array ('url' => $ cityName, 'type' => '0', 'status' => '1')-> find ();
If ($ cityinfo ){
Cookie ('cityid', $ cityinfo ['id']);
Cookie ('cityurl', $ cityinfo ['url']);
Cookie ('citytitle', $ cityinfo ['title']);
$ This-> city ($ cityName );
} Else {
Cookie ('cityid', null );
Cookie ('cityurl', null );
Cookie ('citytitle', null );
Redirect ('cities ');
}
}
}
// Note that the city method is the protected method.
Protected function city ($ name ){
Pre (session ());
Pre (cookie ());
If (isset ($ _ GET ['P']) {$ p = $ _ GET ['P'];} else {$ p = 1 ;}
$ This-> assign ('P', $ p); // current page number
$ This-> assign ('category ', D ('category')-> categoryList (''); // obtain the category and quantity
// Prevents program prompts from errors, set to 0
$ Info ['categoryid'] = 0; $ info ['category _ 2'] = 0;
$ This-> assign ('area ', D ('categoryare')-> areaList ($ info); // obtain the city region category
$ Data ['city'] = $ name;
If (isset ($ _ GET ['order']) {$ data ['order'] = $ _ GET ['order'];} else {$ data ['order'] = '';}
Pre ($ data );
$ This-> assign ('data', $ data); // obtain the list
$ This-> assign ('team', D ('team')-> teamList_ I ($ data, $ p); // obtain the list
$ This-> assign ('page', D ('team')-> pageshow_ I ($ data, $ p); // obtain the list
$ This-> assign ('pagetype ', ACTION_NAME );
$ This-> display ('index/Index ');
}
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB