phpstorm开发工具和tp5.0.24搭建api开发基本流程
2021/4/15 22:25:27
本文主要是介绍phpstorm开发工具和tp5.0.24搭建api开发基本流程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
下载
安装5.0最新版5.0.24
composer create-project topthink/think tp 5.0.*
隐藏入口文件index.php
public目录下.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>
改为
<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>
开启调试模式
找到application/config.php文件
开启调试模式
// 应用调试模式 'app_debug' => true
restful要求
域名部署(可选)
比如 你的域名 www.abc.com 你希望你的api通过 adminapi.abc.com 来进行开发就可以通过这样
如果你写api更新是喜欢通过二级域名访问就可以这样做,开启下面的选项
// 域名部署 'url_domain_deploy' => true
然后就需要在application/route.php下
参考如下代码
use think\Route; //后台接口域名路由 adminapi Route::domain('adminapi', function(){ //adminapi模块首页路由 Route::get('/', 'adminapi/index/index'); //定义 域名下的其他路由 //比如 以后定义路由 get请求 http://adminapi.pyg.com/goods 访问到 adminapi模块Goods控制器index方法 //Route::resource('goods', 'adminapi/goods'); //获取验证码接口 Route::get('captcha/:id', "\\think\\captcha\\CaptchaController@index"); Route::get('captcha', 'adminapi/login/captcha'); //登录接口 Route::post('login', 'adminapi/login/login'); //退出接口 Route::get('logout', 'adminapi/login/logout'); });
创建数据库
create database if not exists blog default charset utf8mb4 collate utf8_unicode_ci;
跨域测试
postman虽然可以很好的发起请求,但是跨域问题是没法测试的。因为postman根本不会产生跨域问题,好比微信小程序也不用处理跨域问题,浏览器才会有跨域问题
那该如何测试?
把这段代码,放到任意一个网站下的控制台回车即可测试你是否处理了跨域问题,但是要注意,假如你的协议是 http 那么你找的网站也必须是要http的网站
var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.blog.com/api/v1/aaa'); xhr.send(null); xhr.onload = function(e) { var xhr = e.target; console.log(xhr.responseText); }
如果要添加token可以添加如下选项
var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.blog.com/api/v1/aaa'); xhr.setRequestHeader("x-access-token","你的token"); xhr.send(null); xhr.onload = function(e) { var xhr = e.target; console.log(xhr.responseText); }
跨域请求知识点
OPTIONS请求即预检请求,可用于检测服务器允许的http方法。当发起跨域请求时,由于安全原因,触发一定条件时浏览器会在正式请求之前自动先发起OPTIONS请求,即CORS预检请求,服务器若接受该跨域请求,浏览器才继续发起正式请求
CORS预检请求触发条件
- 使用了下面任一HTTP 方法:
PUT/DELETE/CONNECT/OPTIONS/TRACE/PATCH - 人为设置了以下集合之外首部字段:
Accept/Accept-Language/Content-Language/Content-Type/DPR/Downlink/Save-Data/Viewport-Width/Width - Content-Type 的值不属于下列之一:
application/x-www-form-urlencoded、multipart/form-data、text/plain
入口文件处理options
public/index.php
//处理跨域Options预检请求 if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){ //允许的源域名 header("Access-Control-Allow-Origin: *"); //允许的请求头信息 header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization"); //允许的请求类型 header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH'); exit; }
模块创建
php think build --module admin
php think build --module adminapi
php think build --module home
php think build --module common
php think build --module mobile
一般这几个模块就够用了
这篇关于phpstorm开发工具和tp5.0.24搭建api开发基本流程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。
- 2023-11-18centos7编译安装phpMyAdmin教程。
- 2023-10-13唱衰这么多年,PHP 仍然还是你大爷!
- 2023-07-25PHP8,性能更好,语法更好,类型安全更完善