# thinkphp小测试 **Repository Path**: mcyrj/thinkphp-test ## Basic Information - **Project Name**: thinkphp小测试 - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-31 - **Last Updated**: 2024-05-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Thinkphp-test ## 一、开发环境 PhpStorm+phpstudy(nginx+mysql5.7+php7.3+redis6.0) ## 二、 项目搭建 ### 1.1 安装thinphp5.0 在工作目录下,执行 `composer create-project topthink/think thinkphp-test 5.0.*` ### 1.2 绑定域名 修改hosts文件 添加 127.0.0.1 [api.thinkphp-test.com](http://api.thinkphp-test.com/) 并绑定public 为运行目录 ### 1.3 隐藏入口文件 在public目录下的nginx.htaccess文件里添加 ``` if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; break; } ``` ### 1.4 开启路由功能 修改application/config.php ``` 'url_domain_deploy' => true, ``` ## 三、系统设计 ### 1.数据表设计 | 表名 | 备注 | | ------------- | ---------- | | think_news | 新闻表 | | think_content | 新闻详情表 | | | | ### 2.模块设计 | 模块名 | 用途 | | ------------ | ------------------ | | 后台接口模块 | 用来向外部提供接口 | | | | ## 四、开发留言板 ### 1.添加路由 修改application/route.php为 ``` use think\Route; //后台接口路由 Route::domain('api',function(){ //留言板路由 Route::resource('messages','adminapi/messages',[],['id'=>'\d+']); }); ``` ### 2.创建后台接口模块 ``` php think build --module adminapi ``` ### 3.创建留言板控制器 php think make:controller adminapi/Messages ### 4.创建公共控制器 php think make:controller adminapi/Common ### 5.安装jwt 插件 composer require lcobucci/jwt 3.3 ### 6.封装jwt类 extend/tools/jwt/Token.php ### 7.在common控制器中编写代码验证jwt+连接redis ### 8.编写留言板代码 ## 五、开发新闻模块 ### 1.配置数据库 修改application/database.php为 ``` // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'thinkphp-test', // 用户名 'username' => 'root', // 密码 'password' => 'root', // 数据库表前缀 'prefix' => 'think_', ``` ### 2.添加model与控制器 ``` php think make:controller adminapi/News php think make:model adminapi/News php think make:model adminapi/Content ``` ### 3.关联模型 修改application\adminapi\model ``` //关联详情 public function content(){ return $this->hasOne('Content','n_id'); } ``` ### 4.添加news路由 在application/route.php里添加 ``` //新闻路由 Route::resource('news','adminapi/news',[],['id'=>'\d+']); ``` ### 5.编写新闻模块代码