ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

phpstorm开发工具和tp5.0.24搭建api开发基本流程

2021-04-15 21:01:38  阅读:203  来源: 互联网

标签:24 index tp5.0 跨域 adminapi xhr api php 请求


下载

安装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预检请求触发条件

  1. 使用了下面任一HTTP 方法:
    PUT/DELETE/CONNECT/OPTIONS/TRACE/PATCH
  2. 人为设置了以下集合之外首部字段:
    Accept/Accept-Language/Content-Language/Content-Type/DPR/Downlink/Save-Data/Viewport-Width/Width
  3. 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

一般这几个模块就够用了

标签:24,index,tp5.0,跨域,adminapi,xhr,api,php,请求
来源: https://blog.csdn.net/ljh101/article/details/115682096

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有