tp配置+路由+基本操作

摘要:
-e$request_filename){rewrite^(.*)$/index.php?

 一、打开apache 的配置文件httpd_conf添加以下代码

<VirtualHost *:80>
DocumentRoot "D:wwwroot hinkphppublicindex.php"  #网站运行目录
ServerName tp.com #域名
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:wwwroot hinkphppublicindex.php"
ServerName w.tp.com
</VirtualHost>

如果是Nginx环境的话可以在Nginx.conf中添加

location/{

  fi(!-e $request_filename){

    rewrite ^(.*)$ /index.php?s=/$1 last;

    break;

   }

}

二、生成模块

1、找到根目录下的build.php配置如下

// 定义demo模块的自动生成 (按照实际定义的文件名生成)
'admin' => [
'__file__' => ['common.php'],
'__dir__' => ['behavior', 'controller', 'model', 'view'],
'controller' => ['Index', 'Test', 'UserType'],
'model' => ['User', 'UserType'],
'view' => ['index/index'],
],
2、在public目录下index.php文件加入如下代码
$build= include  '../build.php';
hinkBuild::run($build);
3、运行网站此时模块已经生成功,些在删除主放口的代码恢复即可
--------------------
三、基本操作
namespace appindexcontroller; //命名空间
use thinkController;//引用控制器
use thinkRequest;//引用request类
use thinkDb;//引用数据库操作类
class Index extends Controller
{
public function index()
{
$param=$this->request->param();//获取$_GET $_POST所有参数
$data=Db::name("user")->find();//查询user表第一行记录
$this->assign('data',$data);//赋值数组
$this->assign('param',$param);//赋值数组
$this->assign('str','test');//赋值字符
return $this->fetch();//显示模版页,也可以指定模版页$this->fetch('index2')
}
}

 view模块调用方法

{$str}<br/>
用户名:{$data['username']}<br/>也可以额头{$data.username}
----------------------------
四、路由规则

1、地址栏参数解说

http://tp.com/index/index/index/name/aa

/index/index/index/name/aa

index模块/index控制器/index是方法名/个name参数取名/个aa是name参数字段的值

2、控制器首字母大写,如果是驼峰试在地址栏访问时用下划线,如控制器UserName在地址栏访问用

/index/user_name/

3、如果要关掉URL自动转换功能,找到config.php,默认是自动转换URL大小写,关掉的话就用false

// 是否自动转换URL中的控制器和操作名

'url_convert' =>false,

4、第二咱地址访问方式

http://tp.com/?s=index/index/index//name/aa
?s=index模块/index控制器/index是方法名/个name参数取名/个aa是name参数字段的值

5、配置路由

找到route.php下添加以下

'index/[:name]' => ['index/index/index', ['method' => 'get','ext'=>'html']],
说明:
访问的时候http://tp.com/index/index.html 省去index/index/index
其中
index/[:name]表示参数名[]号表示可选,可有可无,index.html中的html变成了参数字传送过去了

6、配置路由带参数的

找到route.php下添加以下

'url/:year/:month' => ['index/index/url', ['method' => 'get','ext'=>'html'],['name'=>'d{4}','pass'=>'d{2}']],
说明:
/:year参数名 /:month参数名 index/index/url路径 ['name'=>'d{4}','pass'=>'d{2}']参数正则限制说明
----

控制器必须要有参数,否则出错
public  function url($year,$month)
{
echo $year.'-'.$month;
//return $this->fetch('url');
}
----
访问地址
http://tp.com/url/2017/10.html 说明 url对因的路由规则名 2017和10被当作控制器方法的参数来传值了


7、配置地址栏参数分隔
// pathinfo分隔符
'pathinfo_depr' => '/', //可以修改成-

8、在控制器下生成url
首先引用use thinkUrl;
public  function url()
{
echo url('fun','a=1&b=2');//说明fun是控制器里的方法,a=1&b=2是要参数
}

免责声明:文章转载自《tp配置+路由+基本操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇数据链路层(3) 流量控制Swoole从入门到入土(17)——WebSocket服务器[成员函数与配置选项]下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

多线程之Nsthread的使用方法

一 NSThread的基本使用 1)NSThread创建的四种方式 第一种 创建方式 alloc initwith...... 特点:需要手动启动线程,可以拿到线程对象进行详细设置 //创建线程 NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selecto...

前端JS下载文件总结

Data URLs         Data URLs: 即前缀为data: 协议的URL,其允许内容创建者向文档中嵌入小文件。   例如:可以直接在HTML中的img元素直接使用Data URLs ; data:[<mediatype>][;base64],<data> mediatype: 是个 MIME 类型的字符串,例如...

IDEA连接SQL Server

package ServletDemo; import java.sql.*; public class Datebase { public static void main(String[] args) throws ClassNotFoundException, SQLException { //加载驱动 C...

6.1 路由router

路由将信息由源地址传递到目的地的一种角色. 一、路由简单应用举个例子: let express=require('express'); let app=express(); app.use(express.static('public')); // GET /home 显示 网站首页 app.get('/home',function(req,res...

关于HTTP协议,一篇就够了

HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。 HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。 HTTP是一个属于应用层的面向对象的协议,由于其简捷...

vue项目中动态加载路由组件this.$route.path获取不正确('/'而不是‘/course’)

 如果是这种方式(方式2),使用this.$route获取当前path是‘/’,而不是‘course’; 如果改为方式1,那就可以取得到了。猜想的原因应该是:项目启动,懒加载方式,路由加载注册的时候没有加载path:'/course'下的$route...