Heim  >  Artikel  >  php教程  >  Eine kurze Diskussion zur Subdomain-Bereitstellung und Routing-Optimierung von ThinkPHP3.2 (1)

Eine kurze Diskussion zur Subdomain-Bereitstellung und Routing-Optimierung von ThinkPHP3.2 (1)

WBOY
WBOYOriginal
2016-12-05 13:26:271660Durchsuche

Vorwort: Der Aufbau eines Website-Systems umfasst häufig mehrere Unterwebsites, z. B. die offizielle Website für den PC, die offizielle Website für Mobilgeräte und die Backend-Verwaltung. Die gesamte Architektur kann aus Sicht von ThinkPHP stammen Grob gesagt ist Model( M) dasselbe. Controller (C) enthält gemeinsam genutzte API-Teile und nicht gemeinsam genutzte Teile, während View während der gesamten anfänglichen Architektur hauptsächlich die folgenden Aspekte berücksichtigen kann:

Gemeinsame Datenbank konfigurieren, Module in der Anwendung erweitern, Ansichtsordner anpassen, Bereitstellung von Subdomainnamen, Vereinfachung des Routings....

Die spezifischen Vorgänge sind wie folgt:

1. ThinkPHP-Umgebung und Datenbank vorbereiten

a. Laden Sie den Quellcode der Vollversion von ThinkPHP3.2 von der offiziellen Website herunter, entpacken Sie ihn, nennen Sie ihn testWeb und legen Sie ihn im www-Verzeichnis ab. Sie können Folgendes sehen:

Das bedeutet, dass es bei der Bereitstellung kein Problem gibt.

b. Bereiten Sie eine Testdatenbank für Bücher vor, erstellen Sie eine neue Buchtabelle (ID, Titel, Preis) und bereiten Sie Testdaten vor

 

c. Konfigurationsdatenbank, siehe ThinkPHP-Konfigurationsladeregeln, Sie können die herkömmliche Konfiguration (ThinkPHP/Conf/convention.php) in Kombination mit der Anwendungskonfiguration (Application/Common/Conf/config.php) und der Modulkonfiguration (Application.php) verwenden /aktueller Modulname/Conf/config.php),

Hier verwende ich die Datenbankkonfiguration in der Anwendungskonfiguration:

<span style="color: #008080"> 1</span>     <span style="color: #008000">//</span><span style="color: #008000">数据库配置信息</span>
<span style="color: #008080"> 2</span>     'DB_TYPE'   => 'mysql', <span style="color: #008000">//</span><span style="color: #008000"> 数据库类型</span>
<span style="color: #008080"> 3</span>     'DB_HOST'   => 'localhost', <span style="color: #008000">//</span><span style="color: #008000"> 服务器地址localhost</span>
<span style="color: #008080"> 4</span>     'DB_NAME'   => 'books', <span style="color: #008000">//</span><span style="color: #008000"> 数据库名</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"> 数据库表前缀</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"> 数据库调试模式 开启后可以记录SQL日志 3.2.3新增<br></span>
Als nächstes fügen Sie eine Aktion im Standard-Index-Controller hinzu:

<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>     }
Besuchen Sie: http://192.168.1.122/testWeb/index.php/

Home/Index/get_book?id=1. Sie können zu den JSON-Daten gehen, die Datenbankverbindung angeben und darauf zugreifen weit Normal:

       

2. Erweitern Sie die Module im Anwendungsverzeichnis

a. Kopieren Sie schnell das aktuelle Home-Modul, benennen Sie es in das H5-Modul um und ändern Sie den Namespace im entsprechenden Controller in: Namespace H5Controller,

Besuchen Sie: http://192.168.1.122/testWeb/index.php/

H5/Index/get_book?id=1. Zu diesem Zeitpunkt wurden die Daten erfolgreich zurückgegeben, was darauf hinweist, dass das aktuelle H5 Modul ist in Ordnung; das gleiche können wir antworten

Erstellen Sie ein Admin-Modul.

3. Benutzerdefinierter Ansichtsordner

a. Ansichtskonzept: ThinkPHP ist ein PHP-Framework, das auf der MVC-Struktur basiert. Wenn es sich um eine einfache Datenbankoperation handelt, kann das Modell im Modul häufig weggelassen werden und CURD-Operationen können mit abgeschlossen werden Nur der Controller ist auch eine der sehr wichtigen Funktionen. Ansichten in ThinkPHP beziehen sich hauptsächlich auf Vorlagendateien und Vorlagen-Engines: Vorlagendateien können einfach als die grundlegenden „kleinen Originale“ beim Bauen von Bausteinspielzeugen verstanden werden. Diese „kleinen Originale“ können unterschiedliche Formen haben und die „kleinen Originale“ von jedem Bei Bausteinen können eine oder mehrere Formen verwendet werden; die Schablonen-Engine dient dazu, die „konkaven“ und konvexen „Schnallen“ in Bausteinspielzeugen zu bilden und so „kleine Originale“ zu kombinieren.

b. Verwendung anzeigen: Fügen Sie den Vorgang Home module-Index controller-book hinzu, um alle Bücher abzurufen, und erstellen Sie einen neuen Indexordner-new book.html im Ansichtsordner des Home module-new book.html

<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> }
Vorlagendatei:

<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>序号:{$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>价格:{$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,此时可以看到页面:

       此时,说明这个视图使用是正确的,但是同时也有一些不好的地方,如果模板文件多起来的时候,就不利于模板修改编辑(目录层次太深了),可以在模块配置中,定义当前模块默认的视图目录(Application/Home/Conf/config.php):

<code class="hljs bash"><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></code>

      类似定义H5、admin模块的视图目录,最终得到的目录组织,到此,就可以大概有几个不同的子网站的初步架构了。而对于网站的分类:二级菜单、三级菜单就可以分别对应控制器(Controller)和操作(action),根据视图模板构造动态的页面:

     

4、子域名部署,关于子域名的解析可以参考我之前的文章网站部署一级域名、二级域名、子域名

    a、主要修改一些相关配置:

<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>

      此时,也就是说,有不同子域名(二级网站别名)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模块下的书籍页面(改Home为H5也都指向手机端页面);

     b、那么如果要根据不同的子域名,直接绑定到不同的模块怎么办?参考ThinkPHP的域名部署,也就是在惯用配置下补充一些信息:

<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"> 子域名部署规则</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 之类的后缀必须设置    </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 则必须设置 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,会发现有以下错误:

    这是因为绑定子域名部署之后,index.php入口文件定位到Application应用目录之前,就已经根据不同的子域名,直接进入相应的目录里面了,这时Home就会被判断要查找的控制器,显然当前控制器只有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,是不是就都分别访问到相应模块下的操作并返回视图呢:

          

     这时,基本就可以搭建了一个大体的网站架构就进一步完善了些,从http://192.168.1.122/testWeb/index.php/H5/Index/book访问手机端书籍页面,到http://m.chqtest.com/index.php/Index/book是不是可以省略了一级资源目录和模块名了呢,

     整个结构也清晰了很多。下一篇,将继续说说ThinkPHP关于路由优化,PC、H5网站相互切换的技巧,有兴趣的可以留意下,关于这次例子可以参考Demo

 

    

 

 

 

 

 

        

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn