Introduction to ThinkPHP3.2 sub-domain deployment and route optimization (I), thinkphp3.2 sub-domain

Source: Internet
Author: User

Introduction to ThinkPHP3.2 sub-domain deployment and route optimization (I), thinkphp3.2 sub-domain

A website system usually contains multiple sub-websites, such as the PC official website, Mobile official website, background management, and data sources from the same database. The entire architecture is ThinkPHP, it can be roughly understood that Model (M) is the same. Controller (C) contains the shared API part and the non-shared part, while View is completely not shared. Here, during the entire initial architecture, we can mainly consider the following aspects:

Configure shared databases, expand modules in the Application, customize view folders, deploy subdomains, and simplify routing ....

The procedure is as follows:

1. Prepare the ThinkPHP Environment and Database

A. Download the full source code of ThinkPHP3.2 on the official website. decompress the package named testWeb and place it in the www directory. visit http: // 192.168.1.122/testWeb/. You can see:

It indicates that the deployment is correct.

B. Prepare a test database, create a book table (id, title, price), and prepare test data;

C. Configure the database. Refer to ThinkPHP to configure the loading rules. You can use the conventional configuration (ThinkPHP/Conf/convention. php), combined with Application configuration (Application/Common/Conf/config. php), module configuration (Application/current Module name/Conf/config. php ),

Here I use the database configuration on the application configuration:

1 // Database Configuration Information 2 'db _ type' => 'mysql', // Database TYPE 3 'db _ host' => 'localhost ', // server address localhost 4 'db _ name' => 'books ', // database NAME 5' DB _ user' => 'root ', // username 6'db _ pwd' => '', // password 7'db _ port' => 3306, // port 8 'db _ prefix' => '', // database table PREFIX 9' DB _ CHARSET '=> 'utf8 ', // Character Set 10'db _ debug' => TRUE, // After the database DEBUG mode is enabled, you can record the SQL log 3.2.3 added

Next, add an action in the default Index controller ):

1     public function get_book($id){2         $param['id'] = I('get.id');3         $model = M('book')->where($param)->find();4         $this->ajaxReturn($model);5     }

Access: http: // 192.168.1.122/testWeb/index. php/Home/Index/get_book? Id = 1, you can go to the json data, indicating that the database connection and access are normal until now:

2. Expand the modules in the Application directory

A. Quickly copy the current Home module, rename it to the H5 module, and change the namespace in the corresponding Controller to namespace H5 \ Controller,

Access: http: // 192.168.1.122/testWeb/index. php/H5/Index/get_book? Id = 1. If data is successfully returned, it indicates that the current H5 module is okay. Similarly, we can

Create an Admin module.

3. Custom view folder

A. View concept: ThinkPHP is a PHP framework designed based on the MVC structure. For simple database operations, you can often omit the Model in the module, you can only use the Controller to complete the CURD operation, and the view is also a very important function. The view in ThinkPHP mainly refers to the template file and template engine: The template file can be simply understood as the basic "Small original" in building block toys. The "Small original" may be of different shapes, one or more small originals of each shape may be used during the construction process. The template engine is used to build the "Concave and convex" buckle in the building block toys, form a rule to combine "Small originals.

B. View usage: preview

1 public function book(){2     $models = M('book')->select();3     $this->assign('books',$models);4     $this->display();5 }

Template file:

1 <body> 2 <volist name = "books" id = "vo"> 3 <p> No.: {$ vo. id} </p> 4 <p> title: {$ vo. title} </p> 5 <p style = "color: # FF0000"> price: {$ vo. price }</p> 6 </volist> 7 </body>

C. Access: http: // 192.168.1.122/testWeb/index. php/Home/Index/book. The page is displayed:

In this case, it indicates that the view is used correctly, but there are also some disadvantages. If the template file is too many, it is not conducive to template modification and editing (the directory level is too deep ), you can define the default view directory (Application/Home/Conf/config. php ):

 define('TMPL_PATH','./Public/PC/'); 

Similar to defining the view directories of the H5 and admin modules, the final directory organization can have a few preliminary architectures of different sub-websites. The classification of websites: Level-2 menus and level-3 menus correspond to controllers and actions respectively, and dynamic pages are constructed based on view templates:

4. subdomain name deployment. For subdomain name resolution, refer to my previous articles.

A. Modify the following configurations:

 1 <VirtualHost *:80> 2     DocumentRoot "E:/wamp/www/testWeb/" 3     ServerName  chqtest.com 4     ServerAlias m.chqtest.com 5     <Directory "E:/wamp/www/testWeb/"> 6     Allow from all       7     </Directory> 8     <IfModule dir_module> 9        DirectoryIndex  mobile.php index.html index.htm default.php default.htm default.html10     </IfModule>11 </VirtualHost>12 13 <VirtualHost *:80>14     DocumentRoot "E:/wamp/www/testWeb/"15     ServerName  chqtest.com16     ServerAlias www.chqtest.com17     <Directory "E:/wamp/www/testWeb/">18     Allow from all      19     </Directory>20     <IfModule dir_module>21        DirectoryIndex  index.php index.html index.htm default.php default.htm default.html22     </IfModule>23 </VirtualHost>24 25 <VirtualHost *:80>26     DocumentRoot "E:/wamp/www/testWeb/"27     ServerName  chqtest.com28     ServerAlias admin.chqtest.com29     <Directory "E:/wamp/www/testWeb/">30     Allow from all      31     </Directory>32     <IfModule dir_module>33        DirectoryIndex  index.php index.html index.htm default.php default.htm default.html34     </IfModule>35 </VirtualHost>

At this point, that is to say, there are different sub-domain names (second-level website alias) www.chqtest.com m.chqtest.com admin.chqtest.com can be directly accessed to www/testWeb directory, such as http://www.chqtest.com/index.php/Home/Index/book,

Http://m.chqtest.com/index.php/home/index/bookare all similar. They only go to the Home page (the Home page also points to the mobile phone page when it is changed to H5 );

B. What if we need to bind different subdomains to different modules? Refer to the ThinkPHP domain name deployment, that is, to add some information under the usual Configuration:

1 'app _ SUB_DOMAIN_DEPLOY '=> true, // enable sub-domain deployment 2 // complete domain name deployment 3 'app _ SUB_DOMAIN_RULES '=> array (// sub-domain name deployment rule 4 'www .chqtest.com' => 'home ', // The domain name www.chqtest.com points to the Home module 5' m .chqtest.com '=> 'h5', 6' admin .chqtest.com' => 'admin', 7 ), 8 'app _ DOMAIN_SUFFIX '=> '', // If the domain name SUFFIX is a SUFFIX such as com.cn net.cn, you must set 'Action _ SUFFIX' => '', // operation method suffix 10' MULTI _ MODULE '=> true, // whether multiple modules are allowed. If the value is false, DEFAULT_MODULE11 'module _ DENY_LIST '=> array ('common', 'runtime') must be set '), 12 'module _ ALLOW_LIST '=> array ('home', 'h5', 'admin'), // list of accessible modules

Access ingress again:

This is because after the BIND sub-domain name is deployed, index. before locating the php entry file to the Application directory, you can directly access the corresponding directory based on different sub-domain names. At this time, Home will be judged as the Controller to be searched, obviously, the current controller only has an Index, so re-Access: Allow:

At this time, we can build a General website architecture to further improve it, from http: // 192.168.1.122/testWeb/index. php/H5/Index/book: Visit the mobile phone book page at http://m.chqtest.com/index.php/index/book. you can skip the first-level resource directory and register name,

The entire structure is much clearer. Next, I will continue to talk about ThinkPHP's skills on route optimization and switching between PCs and H5 websites. If you are interested, please pay attention to it. For this example, refer to the Demo.

 

 

 

 

 

 

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.