thinkphp URL規則、URL偽靜態、URL路由、URL重寫、URL產生(十五)_PHP教程

來源:互聯網
上載者:User

thinkphp URL規則、URL偽靜態、URL路由、URL重寫、URL產生(十五)


本章節:詳細介紹thinkphp URL規則、URL偽靜態、URL路由、URL重寫、URL產生

一、URL規則
1、預設是區分大小寫
2、如果我們不想區分大小寫可以改設定檔
'URL_CASE_INSENSITIVE'=>true,//url不區分大小寫
*模組命名太長的情況:
A、如果模組名為 UserGroupAction,複雜模組(一般是IndexAction)
那麼url找模組就必要要寫成
http://localhost/thinkphp4/index.php/user_group/index
B、如果'URL_CASE_INSENSITIVE'=>false(區分大小寫情況可以訪問)
那麼url也可以寫為
http://localhost/thinkphp4/index.php/UserGroup/index


二、URL偽靜態(tp預設支援偽靜態)
http://localhost/thinkphp4/index.php/UserGroup/index.xml
*預設pdo、html、xml...都是支援的,如果要限制加個配置就行
'URL_HTML_SUFFIX'=>'html|shtml|xml',//限制偽靜態尾碼


三、URL路由
1、啟動路由
要在設定檔中開啟路由支援
'URL_ROUTER_ON' => true, //開啟路由
2、使用路由
1.規則運算式配置路由
'URL_ROUTER_ON' => true, //開啟路由
'URL_ROUTE_RULES' => array(
'my'=>'Index/index',//靜態地址路由 訪問:http://localhost/thinkphp/index.php/my
':id/:num'=>'Index/index',/*後面的數字值隨便寫,字母也行
動態地址路由 訪問:http://localhost/thinkphp/index.php/10/200
可以再模組控制器中用get方式傳值 或 擷取
echo $_GET['id'];
echo $_GET['num'];
*/
'my/:num'=>'Index/index', //動靜混合地址路由 http://localhost/thinkphp/index.php/my/200
'year/:year/:month/:date'=>'Index/index',//動態和靜態混合地址路由: http://localhost/thinkphp/index.php/year/2014/12/21
'year/:year\d/:month\d/:date\d'=>'Index/index',//動態和靜態混合地址路由 --加上 \d代表類型只能是數字
'my/:id$'=>'Index/index',// 加上$說明地址中只能是 my/1000 後面不能有其他內容了
);


2.Regex配置路由
//http://localhost/thinkphp/index.php/year/2014/12/21
'/^year\/(\d{4})\/(\d{2})\/(\d{2})/'=>'Index/index?year=:1&month=:2&date=:3'


3、注意事項:
1.越複雜的路由越往前面放
'URL_ROUTE_RULES'=>array(
'my/:year/:month:/:day'=>'Index/day', *複雜的路由放在前面,放後面就不會執行
'my/:id\d'=>'Index/index',
'my/:name'=>'Index/index',
)
2.可以使用$作為完全符合的路由規則(不管複雜否,都會匹配所有正則)
'URL_ROUTE_RULES'=>array(
'my/:id\d$'=>'Index/index',
'my/:name$'=>'Index/index',
'my/:year/:month:/:day$'=>'Index/day',
),
3.用正則匹配的方式
'URL_ROUTE_RULES'=>array(
'/^my\/(\d+)$/'=>'Index/index?id=:1',
'/^my\/(\w+)$/'=>'Index/index?name=:1',
'/^my\/(\d{4})\/(\d{2})\/(\d{2})$/'=>'Index/day?year=:1&month=:2&day=:3',
),


四、URL重寫
比如:http://localhost/thinkphp/index.php/Index/index.html/t/my ---- 不想讓index.php出現
下面是Apache的配置過程,可以參考下:
1、httpd.conf設定檔中載入了mod_rewrite.so模組
2、AllowOverride None 將None改為 All
3、確保URL_MODEL設定為2 (該步驟省略)
4、把下面的內容儲存為.htaccess檔案放到入口檔案的同級目錄下

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

重啟Apache之後,原來的
就可以通過訪問
http://localhost/thinkphp/Index/index.html/t/my --簡化了URL地址,對SEO的支援度好點


五、URL產生(手冊有詳細介紹)
public function url(){
echo U('Index/add'); // 產生Index模組的add操作的URL地址
///thinkphp/index.php/index/add
}

上一篇http://qdxinbj8.2cto.com/index.php?m=content&c=content&a=public_preview&steps=1&catid=75&id=363637

http://www.bkjia.com/PHPjc/934466.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/934466.htmlTechArticlethinkphp URL規則、URL偽靜態、URL路由、URL重寫、URL產生(十五) 本章節:詳細介紹thinkphp URL規則、URL偽靜態、URL路由、URL重寫、URL產生 一、...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.