標籤:driver array 代碼 學習筆記 build 路徑 駝峰 請求 app
一.入門與安裝
1.進入C:\Windows\System32\drivers\etc的hosts檔案中增加
127.0.0.1 www.tp5.com
即訪問這個網域名稱就相當於訪問127.0.0.1
2.修改apache設定檔
增加
<VirtualHost ~E42E:80>DocumentRoot "/home/www/tp5/public"<br />ServerName tp5.com<br /></VirtualHost>
意思為訪問www.tp5.com系統會自動引導到這個public目錄下
3.requery模組是處理$_GET和$_POST的 () 內建請求對象 就是在地址欄後面的那些
4.為什麼訪問的是public下,卻能識別app下的,因為public下有那個靜態檔案
<IfModule mod_rewrite.c>Options +FollowSymlinks -MultiviewsRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]</IfModule>
二.路由學習
1.預設訪問方式
1)http://主機名稱/路徑名 入口檔案 模組 控制器 操作
http://localhost/test/index.php/index/index/index
2)URL訪問路徑大小:
host全部轉為小寫;
控制器首字母自動轉為大寫;
3)控制器為駝峰式 HelloWorld
訪問時候
http://localhost/test/index.php/index/hello_world/index
4)如果想用HelloWorld訪問HelloWorld的話,url_convert => false
5)第二種訪問方式
http://localhost/test/index.php?s=index/index/index
6)現在tp5隻支援這兩種方式
2.參數傳入
1)預設傳入方式:參數可以隨意傳入,變數解析不看重順序。
2)想要省略的傳入的話,更改參數url_param_type = 1
3)現在只能嚴格按照操作方法的定義順序來傳值
http://localhost/test/index.php/index/index/shanghai/thinkphp
意思不用傳變數名
http://localhost/test/index.php/index/index?name=thinkphp&city=shanghai
3隱藏index.php
1)tp5預設在app目錄下有一個.htaccess檔案,自動隱藏了,當然是Apache伺服器下。
2)如果用的phpstudy,規則如下:
<IfModule mod_rewrite.c> Options +FollowSymlinks -MultiviewsRewriteEngine onRewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>
3)如果你使用的apache版本使用上面的方式無法正常隱藏index.php,可以嘗試使用下面的方式配置.htaccess檔案
<IfModule mod_rewrite.c>Options +FollowSymlinks -MultiviewsRewriteEngine onRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]</IfModule>
4)如果是nginx伺服器的話
location /{//......省略部分代碼 if(!-e $request_filename){ rewrite ^(.*)$ /index.php?s=/$1 last; } }
4.定義路由
定義路由的主要原因是便於seo最佳化,畢竟tp5的預設訪問方式是通過多個/來訪問的
在route.php裡面添加一些路由規則,定義路由規則後,原來的URL地址會失效,變成非法請求。
1)
寫在這個return中
return [
//添加路由規則到index控制器的hello操作方法
‘hello/:name‘=>‘index/index/hello‘
]
使用[把]:name括起來,就可以不寫
2)
第二種定義路由的方法,在route.php開頭寫
use think\Route;
Route::rule(‘hello/:name‘,‘index/index/hello‘)
3)參數過多的話 在設定檔中設定 pathinfo_depr => ‘-‘,其他的也行,便於seo最佳化
5.產生url地址,便於引用url
1)使用Url::Build()或者url()來產生url地址,建議用url,以下這四種方式輸出結果相同
Url::Build("url2","a=1&b=2");
url("url2","a=1&b=2");
url("url2",["a"=>"1","b"=>"2"]);
url("url2",array("a"=>"1","b"=>"2"));
2)"url2",寫成這樣,會預設的引用當前模組下的當前控制器下的當前方法
3)url("admin/hello/index","a=1&b=2");這樣會產生admin模組下的hello控制器下的index操作
4)url(today/2017/20) 路由規則下有的 ,會產生相應的url地址
5)如果設定了url_html_suffix參數的話,產生的URL地址會帶上尾碼,
‘url_html_suffix‘ => ‘html‘
thinkphp學習筆記(一)