码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp学习笔记(一)

时间:2016-12-21 02:42:52      阅读:244      评论:0      收藏:0      [点我收藏+]

标签: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 -Multiviews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ 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 -Multiviews
RewriteEngine on
RewriteCond %{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 -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ 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学习笔记(一)

标签:driver   array   代码   学习笔记   build   路径   驼峰   请求   app   

原文地址:http://www.cnblogs.com/qiye5757/p/6200832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!