thinkPHP5.0 路由 前后端分離 綁定模塊 隱藏入口文件


 1.前后端分離

  a、在網站public目錄下新建admin.php

  b、打開admin.php

        <?php
// [ 應用入口文件 ]
// 定義應用目錄
      define('APP_PATH', __DIR__ . '/../application/');
      // 加載框架引導文件
      require __DIR__ . '/../thinkphp/start.php';

2.綁定模塊

  1、實現功能

    index.php 這個入口文件 只能去前台模塊

    admin.php 這個入口文件 只能去后台模塊   建議后台的入口文件稍微復雜些

  2、如何實現

    在入口文件中  

define('BIND_MODULE','index');//綁定前台模塊

 

define('BIND_MODULE','admin');//綁定后台模塊

  3、url地址

    a、入口綁定模塊之前

      http://www.tp5.com/入口文件/模塊/控制器/操作

    b、入口綁定模塊之后

      http://www.tp5.com/入口文件/控制器/操作

3.隱藏入口文件

  1、開始apache 的重寫   F:\phpStudy\PHPTutorial\Apache\conf\httpd.conf

    # 把注釋去掉      

    更改后:LoadModule rewrite_module modules/mod_rewrite.so

  2、設置訪問權限    AllowOverride       none改為All

  3、入口文件 ,在網站public目錄下新建.htaccess  

//phpstudy的第一種寫法
<IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule>
//第一種方法不好使的話 使用第二種方法
<IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>

 

  4、重啟服務 

  5、url地址變化

    a、隱藏之前

       http://www.tp5.com/index.php/Index/test

    b、隱藏之后

      http://www.tp5.com/Index/test

tp5.0路由學習注意:路由的優點:1.簡化url地址,方便記憶   2.有利於搜索引擎的優化

  Route::rule('路由表達式','路由地址','請求類型','路由參數(數組)','變量規則(數組)');

  1.支持三種方式的url解析規則

  2.路由只針對應用,不針對模塊,因此路由的設置也是針對應用下面的所有模塊。

  3.如果有些模塊不想使用路由   關閉后台模塊,在后台入口文件添加下面這句話,寫在

加載框架引導文件之后  否則報錯
// 加載框架引導文件
require __DIR__ . '/../thinkphp/start.php';

 

//關閉admin模塊下的路由
\think\App::route(false);

 

   4、路由模式

      a、普通模式

          1.關閉路由,完全使用默認的PATH_INFO方式URL

          2.形式:http://www.tp5.com/admin.php/Index/index

          3.如何配置:

             //是否開啟路由

              ‘url_route_on’    => false,

            //是否強制使用路由

              'url_route_must'  => false,

      b、混合模式

          1.定義:開啟路由,並使用路由定義+默認 PATH_INFO 方式的混合

          2.如何設置

             //是否開啟路由

              ‘url_route_on’    => true,

            //是否強制使用路由

              'url_route_must'  => false,

      c、強制模式 

          1.定義:

            開啟路由,並設置必須定義路由才能訪問

          2.如何設置 

             //是否開啟路由

              ‘url_route_on’    => true,

            //是否強制使用路由

              'url_route_must'  =>true, 

    5、設置路由-動態單個注冊

        a、設置路由文件

          application\route.php 

        b.、如何設置

//引入系統類
use think\Route;
//定義路由規則
//設置了路由之后,就不能使用PATH_INFO模式訪問了
//注冊路由訪問到index模塊Index控制器index方法 Route::rule('/','index/Index/index');
//注冊路由訪問到index模塊Index控制器test方法
Route::rule('test','index/Index/test');

         c、路由的形式

          1.靜態地址路由

            // 注冊路由test  訪問到Index模塊index控制器test方法

              Route::rule('test','index/Index/test');

          2.給路由帶參數

            //注冊帶參數路由

            //http://www.tp5.com/course/1   路由模式

            //http://www.tp5.com/index/Index/course/id/1  普通模式

            Route::rule('course/:id','index/Index/course');

            //如果路由設置兩個參數,必須帶兩個參數

              Route::rule('time/:year/:mouth','index/Index/shijian');

          3.可選參數路由

            //http://www.tp5.com/time/2017

            //http://www.tp5.com/time/2018/5

            Route::rule('time/:year/[:mouth]','index/Index/shijian');

           4、全動態路由(不建議使用)       

              Route::rule(':a/:b','index/Index/dongtai');

 

          5.完全匹配路由     

              //http://www.tp5.com/wanquan   可以訪問
              //http"//www.tp5.com/wanquan/ada/asf/a/f 不可以訪問    

              Route::rule('wanquan$','index/Index/wanquan');

 

          6.路由額外帶參數

              Route::rule('test1','index/Index/test1?id=12&name=adasfds');

 

    

        d、設置請求類型   

          1、tp中請求類型

              get、post、put、delete

          2、Route::rule()  默認支持所有請求類型 

          3、設置所有請求  

//支持get請求
Route::rule('type','index/Index/type','get');
Route::get('type','index/Index/type');

//支持post請求
Route::rule('type','index/Index/type','post');
Route::post('type','index/Index/type');

//同時支持get和post
Route::rule('type','index/Index/type','get|post');

//支持所有請求
Route::rule('type','index/Index/type','*');
Route::any('type','index/Index/type');

//支持put請求
Route::rule('type','index/Index/type','put');
Route::put('type','index/Index/type');

//支持delete請求
Route::rule('type','index/Index/type','delete');
Route::delete('type','index/Index/type');

 

 

       4、如何模擬put和delete請求

<form action="" method="post">  post 重要
    <p>
        <input type="hidden" name="_method" value="PUT"> **隱藏域重要
        <input type="text" name="name">
    </p>
    <p>
        <input type="submit" value="提交">
    </p>
</form>

 

    6、設置路由-動態批量注冊

      a、基本格式

Route::rule([
'路由規則1'=>'路由地址和參數',
'路由規則2'=>['路由地址和參數','匹配參數(數組)','變量規則(數組)']
...
],'','請求類型','匹配參數(數組)','變量規則');

 

      b、使用    

Route::rule([
    'test'   =>  'index/Index/test',
    'course/:id'  =>  'index/Index/course'
]);
Route::get([
    'test'   =>  'index/Index/test',
    'course/:id'  =>  'index/Index/course'
]);

    7、設置路由-配置文件批量注冊

return [
    'test'   =>  'index/Index/test',
    'course/:id'  =>  'index/Index/course'
];

    8、變量規則   

//設置路由參數id必須是數字,必須1到3位
Route::rule('course/:id','index/Index/course','get',[],["id" => "\d{1,3}"]);

    9、路由參數

//路由參數method  請求方式必須是get
//路由參數ext 主要設置路由的后綴
Route::rule('course/:id','index/Index/course','get',['method'=>'get','ext'=>'html'],["id" => "\d{1,3}"]);

 

     10、資源路由

      a、后台功能

        add頁面、展示頁面、刪除功能、修改頁面、修改功能、增加功能

       a、聲明資源路由    

Route::resource('blog','index/Blog');

 

       b、會自動注冊七個路由規則

    11、設置快捷路由

      a、聲明

Route::controller('user','index/User');

 

       b、使用: 

namespace app\index\controller;

class User {
    public function getInfo()
    {
    }
    
    public function getPhone()
    {
    }
    
    public function postInfo()
    {
    }
    
    public function putInfo()
    {
    }
    
    public function deleteInfo()
    {
    }
}

 

       c、URL訪問

get http://localhost/user/info
get http://localhost/user/phone
post http://localhost/user/info
put http://localhost/user/info
delete http://localhost/user/info

    12、生成URL地址

       a、系統類

        use think\url

        url::build('index/Index/index');

       b、系統方法

        url('index/Index/index');

       c、使用

          看手冊詳細介紹

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM