国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) A brief discussion on subdomain deployment and routing optimization of ThinkPHP3.2 (1)

A brief discussion on subdomain deployment and routing optimization of ThinkPHP3.2 (1)

Dec 05, 2016 pm 01:26 PM

Foreword: Building a website system often contains multiple sub-websites, such as PC official website, mobile official website, and backend management. The data comes from the same database. The entire architecture, from the perspective of ThinkPHP, can be roughly understood as Model(M) It’s the same. Controller (C) contains shared API parts and non-shared parts, while View is completely non-shared. During the entire initial architecture, we can mainly consider the following aspects:

Configure shared database, extend modules in Application, customize view folders, subdomain deployment, simplification of routing....

The specific operations are as follows:

1. Prepare ThinkPHP environment and database

a. Download the full version of ThinkPHP3.2 source code from the official website, unzip it and name it testWeb and place it in the www directory. Visit http://192.168.1.122/testWeb/ and you can see:

It means there is no problem with the deployment.

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

??????????????? c. Configure the database, refer to the ThinkPHP configuration loading rules, you can use the conventional configuration (ThinkPHP/Conf/convention.php), combined with the application configuration (Application/Common/Conf/config.php), module configuration (Application/current module name/ Conf/config.php),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? we use the database configuration for the application configuration:

<span style="color: #008080"> 1</span>     <span style="color: #008000">//</span><span style="color: #008000">數(shù)據(jù)庫(kù)配置信息</span>
<span style="color: #008080"> 2</span>     'DB_TYPE'   => 'mysql', <span style="color: #008000">//</span><span style="color: #008000"> 數(shù)據(jù)庫(kù)類型</span>
<span style="color: #008080"> 3</span>     'DB_HOST'   => 'localhost', <span style="color: #008000">//</span><span style="color: #008000"> 服務(wù)器地址localhost</span>
<span style="color: #008080"> 4</span>     'DB_NAME'   => 'books', <span style="color: #008000">//</span><span style="color: #008000"> 數(shù)據(jù)庫(kù)名</span>
<span style="color: #008080"> 5</span>     'DB_USER'   => 'root', <span style="color: #008000">//</span><span style="color: #008000"> 用戶名</span>
<span style="color: #008080"> 6</span>     'DB_PWD'    => '', <span style="color: #008000">//</span><span style="color: #008000"> 密碼</span>
<span style="color: #008080"> 7</span>     'DB_PORT'   => 3306, <span style="color: #008000">//</span><span style="color: #008000"> 端口</span>
<span style="color: #008080"> 8</span>     'DB_PREFIX' => '', <span style="color: #008000">//</span><span style="color: #008000"> 數(shù)據(jù)庫(kù)表前綴</span>
<span style="color: #008080"> 9</span>     'DB_CHARSET'=> 'utf8', <span style="color: #008000">//</span><span style="color: #008000"> 字符集</span>
<span style="color: #008080">10</span>     'DB_DEBUG'  =>  <span style="color: #0000ff">TRUE</span>, <span style="color: #008000">//</span><span style="color: #008000"> 數(shù)據(jù)庫(kù)調(diào)試模式 開啟后可以記錄SQL日志 3.2.3新增<br></span>

Next, add an action in the default Index controller:

<span style="color: #008080">1</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> get_book(<span style="color: #800080">$id</span><span style="color: #000000">){
</span><span style="color: #008080">2</span>         <span style="color: #800080">$param</span>['id'] = I('get.id'<span style="color: #000000">);
</span><span style="color: #008080">3</span>         <span style="color: #800080">$model</span> = M('book')->where(<span style="color: #800080">$param</span>)-><span style="color: #000000">find();
</span><span style="color: #008080">4</span>         <span style="color: #800080">$this</span>->ajaxReturn(<span style="color: #800080">$model</span><span style="color: #000000">);
</span><span style="color: #008080">5</span>     }

   Visit: 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 so far:

???????????????????????????????????????????????????????????????????????????????????????

2. Expand the modules in the Application directory

a. Quickly copy the current Home module, rename it to H5 module and modify the namespace in the corresponding controller to: namespace H5Controller,

Visit: http://192.168.1.122/testWeb/index.php/

H5

/Index/get_book?id=1. At this time, the data is successfully returned, indicating that the current H5 module is OK; similarly we can restore Make an Admin module.

3. Customized view folder

???????????????????????????????????????????????????????????????????????????????????????????????? off out important features. Views in ThinkPHP mainly refer to template files and template engines: template files can be simply understood as the basic "small originals" in building building block toys. These "small originals" may be of different shapes, and each shape of "small originals" may be used in building building blocks. One or more are used in the process; the template engine is to build the "concave and convex" buckles in building block toys, forming a rule to combine "small originals".

b. View usage: Add the Home module-Index controller-book operation to obtain all books, and create a new Index folder-new book.html under the View folder of the Home module

<span style="color: #008080">1</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> book(){
</span><span style="color: #008080">2</span>     <span style="color: #800080">$models</span> = M('book')-><span style="color: #000000">select();
</span><span style="color: #008080">3</span>     <span style="color: #800080">$this</span>->assign('books',<span style="color: #800080">$models</span><span style="color: #000000">);
</span><span style="color: #008080">4</span>     <span style="color: #800080">$this</span>-><span style="color: #000000">display();
</span><span style="color: #008080">5</span> }

Template file:
<span style="color: #008080">1</span> <span style="color: #0000ff"><</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>
<span style="color: #008080">2</span>     <span style="color: #0000ff"><</span><span style="color: #800000">volist </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="books"</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="vo"</span><span style="color: #0000ff">></span>
<span style="color: #008080">3</span>         <span style="color: #0000ff"><</span><span style="color: #800000">p</span><span style="color: #0000ff">></span>序號(hào):{$vo.id}<span style="color: #0000ff"></</span><span style="color: #800000">p</span><span style="color: #0000ff">></span>
<span style="color: #008080">4</span>         <span style="color: #0000ff"><</span><span style="color: #800000">p</span><span style="color: #0000ff">></span>書名:{$vo.title}<span style="color: #0000ff"></</span><span style="color: #800000">p</span><span style="color: #0000ff">></span>
<span style="color: #008080">5</span>         <span style="color: #0000ff"><</span><span style="color: #800000">p </span><span style="color: #ff0000">style</span><span style="color: #0000ff">="color: #FF0000"</span><span style="color: #0000ff">></span>價(jià)格:{$vo.price}<span style="color: #0000ff"></</span><span style="color: #800000">p</span><span style="color: #0000ff">></span>
<span style="color: #008080">6</span>     <span style="color: #0000ff"></</span><span style="color: #800000">volist</span><span style="color: #0000ff">></span>
<span style="color: #008080">7</span> <span style="color: #0000ff"></</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>

????? c、 訪問:http://192.168.1.122/testWeb/index.php/Home/Index/book,此時(shí)可以看到頁(yè)面:

?????? 此時(shí),說明這個(gè)視圖使用是正確的,但是同時(shí)也有一些不好的地方,如果模板文件多起來的時(shí)候,就不利于模板修改編輯(目錄層次太深了),可以在模塊配置中,定義當(dāng)前模塊默認(rèn)的視圖目錄(Application/Home/Conf/config.php):

<span class="hljs-string"><span class="hljs-string"> <span class="cnblogs_code"><span style="color: #008080">define</span>('TMPL_PATH','./Public/PC/'); </span><br></span></span>

類似定義H5、admin模塊的視圖目錄,最終得到的目錄組織,到此,就可以大概有幾個(gè)不同的子網(wǎng)站的初步架構(gòu)了。而對(duì)于網(wǎng)站的分類:二級(jí)菜單、三級(jí)菜單就可以分別對(duì)應(yīng)控制器(Controller)和操作(action),根據(jù)視圖模板構(gòu)造動(dòng)態(tài)的頁(yè)面:

4、子域名部署,關(guān)于子域名的解析可以參考我之前的文章網(wǎng)站部署一級(jí)域名、二級(jí)域名、子域名

a、主要修改一些相關(guān)配置:

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><</span><span style="color: #800000">VirtualHost </span><span style="color: #ff0000">*:80</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 2</span> <span style="color: #000000">    DocumentRoot "E:/wamp/www/testWeb/"
</span><span style="color: #008080"> 3</span> <span style="color: #000000">    ServerName  chqtest.com
</span><span style="color: #008080"> 4</span> <span style="color: #000000">    ServerAlias m.chqtest.com
</span><span style="color: #008080"> 5</span>     <span style="color: #0000ff"><</span><span style="color: #800000">Directory </span><span style="color: #ff0000">"E:/wamp/www/testWeb/"</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span> <span style="color: #000000">    Allow from all      
</span><span style="color: #008080"> 7</span>     <span style="color: #0000ff"></</span><span style="color: #800000">Directory</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 8</span>     <span style="color: #0000ff"><</span><span style="color: #800000">IfModule </span><span style="color: #ff0000">dir_module</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 9</span> <span style="color: #000000">       DirectoryIndex  mobile.php index.html index.htm default.php default.htm default.html
</span><span style="color: #008080">10</span>     <span style="color: #0000ff"></</span><span style="color: #800000">IfModule</span><span style="color: #0000ff">></span>
<span style="color: #008080">11</span> <span style="color: #0000ff"></</span><span style="color: #800000">VirtualHost</span><span style="color: #0000ff">></span>
<span style="color: #008080">12</span> 
<span style="color: #008080">13</span> <span style="color: #0000ff"><</span><span style="color: #800000">VirtualHost </span><span style="color: #ff0000">*:80</span><span style="color: #0000ff">></span>
<span style="color: #008080">14</span> <span style="color: #000000">    DocumentRoot "E:/wamp/www/testWeb/"
</span><span style="color: #008080">15</span> <span style="color: #000000">    ServerName  chqtest.com
</span><span style="color: #008080">16</span> <span style="color: #000000">    ServerAlias www.chqtest.com
</span><span style="color: #008080">17</span>     <span style="color: #0000ff"><</span><span style="color: #800000">Directory </span><span style="color: #ff0000">"E:/wamp/www/testWeb/"</span><span style="color: #0000ff">></span>
<span style="color: #008080">18</span> <span style="color: #000000">    Allow from all      
</span><span style="color: #008080">19</span>     <span style="color: #0000ff"></</span><span style="color: #800000">Directory</span><span style="color: #0000ff">></span>
<span style="color: #008080">20</span>     <span style="color: #0000ff"><</span><span style="color: #800000">IfModule </span><span style="color: #ff0000">dir_module</span><span style="color: #0000ff">></span>
<span style="color: #008080">21</span> <span style="color: #000000">       DirectoryIndex  index.php index.html index.htm default.php default.htm default.html
</span><span style="color: #008080">22</span>     <span style="color: #0000ff"></</span><span style="color: #800000">IfModule</span><span style="color: #0000ff">></span>
<span style="color: #008080">23</span> <span style="color: #0000ff"></</span><span style="color: #800000">VirtualHost</span><span style="color: #0000ff">></span>
<span style="color: #008080">24</span> 
<span style="color: #008080">25</span> <span style="color: #0000ff"><</span><span style="color: #800000">VirtualHost </span><span style="color: #ff0000">*:80</span><span style="color: #0000ff">></span>
<span style="color: #008080">26</span> <span style="color: #000000">    DocumentRoot "E:/wamp/www/testWeb/"
</span><span style="color: #008080">27</span> <span style="color: #000000">    ServerName  chqtest.com
</span><span style="color: #008080">28</span> <span style="color: #000000">    ServerAlias admin.chqtest.com
</span><span style="color: #008080">29</span>     <span style="color: #0000ff"><</span><span style="color: #800000">Directory </span><span style="color: #ff0000">"E:/wamp/www/testWeb/"</span><span style="color: #0000ff">></span>
<span style="color: #008080">30</span> <span style="color: #000000">    Allow from all      
</span><span style="color: #008080">31</span>     <span style="color: #0000ff"></</span><span style="color: #800000">Directory</span><span style="color: #0000ff">></span>
<span style="color: #008080">32</span>     <span style="color: #0000ff"><</span><span style="color: #800000">IfModule </span><span style="color: #ff0000">dir_module</span><span style="color: #0000ff">></span>
<span style="color: #008080">33</span> <span style="color: #000000">       DirectoryIndex  index.php index.html index.htm default.php default.htm default.html
</span><span style="color: #008080">34</span>     <span style="color: #0000ff"></</span><span style="color: #800000">IfModule</span><span style="color: #0000ff">></span>
<span style="color: #008080">35</span> <span style="color: #0000ff"></</span><span style="color: #800000">VirtualHost</span><span style="color: #0000ff">></span>

此時(shí),也就是說,有不同子域名(二級(jí)網(wǎng)站別名)www.chqtest.com m.chqtest.com admin.chqtest.com都可以直接訪問到www/testWeb目錄下,如http://www.chqtest.com/index.php/Home/Index/book,

http://m.chqtest.com/index.php/Home/Index/book都是一樣的,只是訪問到Home模塊下的書籍頁(yè)面(改Home為H5也都指向手機(jī)端頁(yè)面);

b、那么如果要根據(jù)不同的子域名,直接綁定到不同的模塊怎么辦?參考ThinkPHP的域名部署,也就是在慣用配置下補(bǔ)充一些信息:

<span style="color: #008080"> 1</span>     'APP_SUB_DOMAIN_DEPLOY' =>  <span style="color: #0000ff">true</span>,   <span style="color: #008000">//</span><span style="color: #008000"> 是否開啟子域名部署
</span><span style="color: #008080"> 2</span> <span style="color: #008000">    //完整域名部署</span>
<span style="color: #008080"> 3</span>     'APP_SUB_DOMAIN_RULES'    =>    <span style="color: #0000ff">array</span>( <span style="color: #008000">//</span><span style="color: #008000"> 子域名部署規(guī)則</span>
<span style="color: #008080"> 4</span>         'www.chqtest.com'  => 'Home',   <span style="color: #008000">//</span><span style="color: #008000"> www.chqtest.com域名指向Home模塊</span>
<span style="color: #008080"> 5</span>         'm.chqtest.com'   => 'H5',
<span style="color: #008080"> 6</span>         'admin.chqtest.com' => 'Admin',
<span style="color: #008080"> 7</span>     ),
<span style="color: #008080"> 8</span>     'APP_DOMAIN_SUFFIX'     =>  '', <span style="color: #008000">//</span><span style="color: #008000"> 域名后綴 如果是com.cn net.cn 之類的后綴必須設(shè)置    </span>
<span style="color: #008080"> 9</span>     'ACTION_SUFFIX'         =>  '', <span style="color: #008000">//</span><span style="color: #008000"> 操作方法后綴</span>
<span style="color: #008080">10</span>     'MULTI_MODULE'          =>  <span style="color: #0000ff">true</span>, <span style="color: #008000">//</span><span style="color: #008000"> 是否允許多模塊 如果為false 則必須設(shè)置 DEFAULT_MODULE</span>
<span style="color: #008080">11</span>     'MODULE_DENY_LIST'      =>  <span style="color: #0000ff">array</span>('Common','Runtime'),
<span style="color: #008080">12</span>     'MODULE_ALLOW_LIST'    =>    <span style="color: #0000ff">array</span>('Home','H5','Admin'), <span style="color: #008000">//</span><span style="color: #008000"> 允許訪問的模塊列表</span>

???????????? 再次訪問http://www.chqtest.com/index.php/Home/Index/book,http://m.chqtest.com/index.php/Home/Index/book,會(huì)發(fā)現(xiàn)有以下錯(cuò)誤:

??? 這是因?yàn)榻壎ㄗ佑蛎渴鹬?,index.php入口文件定位到Application應(yīng)用目錄之前,就已經(jīng)根據(jù)不同的子域名,直接進(jìn)入相應(yīng)的目錄里面了,這時(shí)Home就會(huì)被判斷要查找的控制器,顯然當(dāng)前控制器只有Index,所以,重新訪問:http://www.chqtest.com/index.php/Index/book,http://m.chqtest.com/index.php/Index/book,http://admin.chqtest.com/index.php/Index/book,是不是就都分別訪問到相應(yīng)模塊下的操作并返回視圖呢:

??????????

???? 這時(shí),基本就可以搭建了一個(gè)大體的網(wǎng)站架構(gòu)就進(jìn)一步完善了些,從http://192.168.1.122/testWeb/index.php/H5/Index/book訪問手機(jī)端書籍頁(yè)面,到http://m.chqtest.com/index.php/Index/book是不是可以省略了一級(jí)資源目錄和模塊名了呢,

???? 整個(gè)結(jié)構(gòu)也清晰了很多。下一篇,將繼續(xù)說說ThinkPHP關(guān)于路由優(yōu)化,PC、H5網(wǎng)站相互切換的技巧,有興趣的可以留意下,關(guān)于這次例子可以參考Demo

?

????

?

?

?

?

?

????????

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)