PDF 下载:
-
ThinkPHP5.0入门实例教程 - 4rwq
-
ThinkPHP5.0快速入门 - qpep
-
ThinkPHP完全开发手册 - 7bae
PHP >= 7.1.0
# 第一次安装
composer create-project topthink/think tp
composer create-project topthink/think=6.0.x-dev tp
# 更新
composer update topthink/framework
# 显示版本
php think version
# 默认端口 8000
php think run
# 设置端口
php think run -p 80
copy .example.env .env
use think\facade\Env;
Env::get('database.username', 'root');
use think\facade\Config;
// 批量设置参数
Config::set(['name1' => 'value1', 'name2' => 'value2'], 'config');
// 获取配置
Config::get('config');
Config::get('config.name1');
// 判断是否存在
Config::has('template');
cache.php
'redis' => [
'type' => 'redis',
'host' => env('redis.host', '127.0.0.1'),
'port' => env('redis.port', '6379'),
'password' => 'redis3.2.100',
'select' => '0',
'expire' => 0,
'prefix' => '',
'timeout' => 0,
],
composer require topthink/think-multi-app
app.php
// 默认应用
'default_app' => 'home',
// 开启应用快速访问
'app_express' => true,
namespace think;
require __DIR__ . '/../vendor/autoload.php';
// 执行HTTP应用 admin 并响应
$http = (new App())->http;
$response = $http->name('admin')->run();
$response = $http->path('path/to/app')->run(); // 对于非自动多应用部署的情况
$response->send();
$http->end($response);
app('http')->getName();
'app_map' => [
'think' => 'admin',
'home' => 'index',
'*' => 'index',
'test' => function($app) {
$app->http->path('path/to/composer/app'); // 使用composer加载应用
},
],
'domain_bind' => [
'blog' => 'blog', // blog子域名绑定到blog应用
'shop.tp.com' => 'shop', // 完整域名绑定
'*' => 'home', // 二级泛域名绑定到home应用
],
'deny_app_list' => ['common']
http://servername/index.php/think
Route::get('think', function () {
return 'hello,ThinkPHP!';
});
// 注册路由到News控制器的read操作
Route::rule('new/:id','News/read');
Route::rule('new/:id', 'News/update', 'GET|POST');
http://servername/new/5 会自动路由到 http://servername/news/read/id/5
// 快捷注册方法:get post put patch delete any
Route::any('new/:id','News/read');
Route::rule('/', 'index'); // 首页访问路由
Route::rule('my', 'Member/myinfo'); // 静态地址路由
Route::rule('blog/:id', 'Blog/read'); // 静态地址和动态地址结合
Route::rule(':user/:blog_id', 'Blog/read'); // 全动态地址
// 可选变量
Route::get('blog/:year/[:month]','Blog/archive');
// 或者
Route::get('blog/<year>/<month?>','Blog/archive');
// 完全匹配
Route::get('new/:cate$', 'News/category');
// 指定生成标识
Route::rule('new/:id','News/read')
->name('new_read');
// 生成路由地址
url('new_read', ['id' => 10]);
// 默认使用路由地址作为路由标识
url('News/read', ['id' => 10]);
cache('name', $value, 3600); // 助手函数
Cache::set('name', $value, 3600);
Cache::set('name', $value, new DateTime('2019-10-01 12:00:00'));
Cache::remember('start_time', time());
Cache::has('start_time'); // 仅判断
Cache::set('name', 1);
Cache::inc('name');
Cache::inc('name', 3);
Cache::dec('name');
Cache::get('name', ''); // 支持指定默认值
cache('name');
Cache::set('name', [1,2,3]);
Cache::push('name', 4);
cache('name', NULL);
Cache::delete('name');
Cache::pull('name'); // 获取并删除
Cache::clear(); // 清空缓存
Cache::store('redis')->set('name','value',3600);
Cache::store('default')->set('name','value',3600);
https://github.com/top-think/think-worker
php.ini
extension=fileinfo
安装命令
composer require topthink/think-worker
# 启动服务端
php think worker
# linux下面可以支持下面指令
php think worker [start|stop|reload|restart|status]
php think worker:server
config/worker_server.php
return [
'socket' => 'http://127.0.0.1:8000',
'name' => 'thinkphp',
'count' => 4,
'onMessage' => function($connection, $data) {
$connection->send(json_encode($data));
},
];
<?php
namespace app\http;
use think\worker\Server;
class Worker extends Server
{
protected $socket = 'http://0.0.0.0:2346';
public function onMessage($connection,$data)
{
$connection->send(json_encode($data));
}
}
config/worker_server.php
return [
'worker_class' => 'app\http\Worker',
];
获取方法 | 目录位置 | 目录说明 |
---|---|---|
root_path() | 根目录 | 项目所在的目录 |
base_path() | 基础目录 | app目录 |
app_path() | 应用目录 | app/应用子目录 |
config_path() | 配置目录 | config |
runtime_path() | 运行时目录 | runtime |