xinchenhui

一、基础:

创建项目:conposer create-project topthink/think tp5 --prefer-dist
创建项目模块:php think build --module demo
访问未配置的路由:http://localhost/tp5/
上线时要关闭调试模式:\'app_debug\' =>  false,   config.php
//创建母案文件需要继承controller类
use think\Controller;

class Index extends Controller($name = \'张三\'){
public function index($name = \'张三\'){
    
$this->assgin(\'name\',$name);//可在html输出{$name}为张三
return $this->fetch();
  }
}
//创建数据表 -- 记录没试过
CREATE TABLE IF NOT EXISTS `think_data`(
    `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
    `data` varchar(255) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;INSERT INTO `think_data`(`id`,`data`) VALUES
(1,\'thinkphp\'),
(2,\'php\'),
(3,\'framework\');
//连接数据库
use think\Db;

class Index extends Controller{
    public function index(){
        $data = Db::name(\'data\')->find();
        $this->assign(\'result\', $data);
        return $this->fetch();
    }
}

二、URL和路由:

1、url访问:

标准的URL访问格式:http://serverName/index.php/模块/控制器/操作/参数名/参数/参数名2/参数2
传入参数还可以使用:
http://serverName/index.php/模块/控制器/操作?参数名=参数&参数名2=参数2
//提示:模块在ThinkPHP中的概念其实就是应用目录下面的子目录,而官方的规范是目录名小写因此模块全部采用小写命名无论URL是否开启大小写转换模块名都会强制小写
//如果你的控制器是驼峰的,那么访问的路径应为:.../hello_world/index
class HelloWorld{
    public function index(){}
}
//如果使用.../HelloWorld/index 则会报错:Helloworld不存在。必须要使用大小写的时候需要配置:
关闭URL自动转换(支持驼峰访问控制器):\'url_convert\' => false,  config.php

2、隐藏index.html

//隐藏路径中的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>
//接下来就可以使用:http://tp5.com/index/index/hello 访问了
//如果使用的是Nginx环境:可以在Nginx.conf中添加:
localtion/{
  if(!-e$request_filename){
    rewrite ^(.*)$ /index.php?s=/$1 last;
    break;
  }
}

3、*定义路由:

//路由定义文件:application/route.php  原来的url将会失效!变为非法请求!
return [\'hello/:name\'=>\'index/index/hello\',];//此方法的 name参数必选!
return [\'hello/[:name]\'=>\'index/index/hello\'];//此方法的 name参数可选!
//动态定义路由:application/route.php
use think\Route;

Rroute::rule(\'hello/:name/:age\',\'index/hello\');//必选参数
Rroute::rule(\'hello/[:name]/[:age]\',\'index/hello\');//可选参数

4、路由参数

//可以用来约束URL后缀条件等。.../hello/index.html有效
return [
    \'hello/[:name]\' => [\'index/hello\', [\'method\' => \'get\', \'ext\' => \'html\']],
];

5、变量规则

//可以用正则表达式来限制路由参数的格式
return [
    \'blog/:year/:month\' => [\'blog/archive\', [\'method\' => \'get\'], [\'year\' => \'\d{4}\', \'month\' => \'\d{2}\']],
    \'blog/:id\'          => [\'blog/get\', [\'method\' => \'get\'], [\'id\' => \'\d+\']],
    \'blog/:name\'        => [\'blog/read\', [\'method\' => \'get\'], [\'name\' => \'\w+\']],
];

6、路由分组


return
[ \'[blog]\' => [ \':year/:month\' => [\'blog/archive\', [\'method\' => \'get\'], [\'year\' => \'\d{4}\', \'month\' => \'\d{2}\']], //blog/archive \':id\' => [\'blog/get\', [\'method\' => \'get\'], [\'id\' => \'\d+\']],                   //blog/get \':name\' => [\'blog/read\', [\'method\' => \'get\'], [\'name\' => \'\w+\']],                 //blog/read ], ];

 

分类:

技术点:

相关文章: