1 .  application目录下新建http/middleware/CrossDomain.php

<?php
/**
* 请求处理中间件
* Author: weiyongqiang<hayixia606@163.com>
* Date: 2018/9/27
* Time: 11:02
*/
namespace app\http\middleware;
use think\Response;
class CrossDomain
{
public function handle($request, \Closure $next)
{
header(‘Access-Control-Allow-Origin: http://localhost:8080’);
header(‘Access-Control-Allow-Credentials: true’);
header(‘Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With’);
header(‘Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE’);
header(‘Access-Control-Max-Age: 1728000’);
if (strtoupper($request->method()) == “OPTIONS”) {
return Response::create()->send();
}
return $next($request);
}
}

2.  需要的模块下middleware.php(与controller目录 同目录):

<?php
/**
* 中间件注册
* Author: weiyongqiang<hayixia606@163.com>
* Date: 2018/9/27
* Time: 11:49
*/
return [
\app\http\middleware\CrossDomain::class
];

  1. 这一部分是请求伪装
  2. // 是否为 GET 请求
  3. if (Request::instance()->isGet()) echo “当前为 GET 请求”;
  4. // 是否为 POST 请求
  5. if (Request::instance()->isPost()) echo “当前为 POST 请求”;
  6. // 是否为 PUT 请求
  7. if (Request::instance()->isPut()) echo “当前为 PUT 请求”;
  8. // 是否为 DELETE 请求
  9. if (Request::instance()->isDelete()) echo “当前为 DELETE 请求”;
  10. // 是否为 Ajax 请求
  11. if (Request::instance()->isAjax()) echo “当前为 Ajax 请求”;
  12. // 是否为 Pjax 请求
  13. if (Request::instance()->isPjax()) echo “当前为 Pjax 请求”;
  14. // 是否为手机访问
  15. if (Request::instance()->isMobile()) echo “当前为手机访问”;
  16. // 是否为 HEAD 请求
  17. if (Request::instance()->isHead()) echo “当前为 HEAD 请求”;
  18. // 是否为 Patch 请求
  19. if (Request::instance()->isPatch()) echo “当前为 PATCH 请求”;
  20. // 是否为 OPTIONS 请求
  21. if (Request::instance()->isOptions()) echo “当前为 OPTIONS 请求”;
  22. // 是否为 cli
  23. if (Request::instance()->isCli()) echo “当前为 cli”;
  24. // 是否为 cgi
  25. if (Request::instance()->isCgi()) echo “当前为 cgi”;
  26. 这一部分是获取当前的信息
  27. $request = Request::instance();
  28. // 获取当前域名
  29. echo domain:  . $request->domain() . ‘<br/>’;
  30. // 获取当前入口文件
  31. echo file:  . $request->baseFile() . ‘<br/>’;
  32. // 获取当前URL地址 不含域名
  33. echo url:  . $request->url() . ‘<br/>’;
  34. // 获取包含域名的完整URL地址
  35. echo url with domain:  . $request->url(true) . ‘<br/>’;
  36. // 获取当前URL地址 不含QUERY_STRING
  37. echo url without query:  . $request->baseUrl() . ‘<br/>’;
  38. // 获取URL访问的ROOT地址
  39. echo root:’ . $request->root() . ‘<br/>’;
  40. // 获取URL访问的ROOT地址
  41. echo root with domain:  . $request->root(true) . ‘<br/>’;
  42. // 获取URL地址中的PATH_INFO信息
  43. echo pathinfo:  . $request->pathinfo() . ‘<br/>’;
  44. // 获取URL地址中的PATH_INFO信息 不含后缀
  45. echo pathinfo:  . $request->path() . ‘<br/>’;
  46. // 获取URL地址中的后缀信息
  47. echo ext:  . $request->ext() . ‘<br/>’;
  48.  
  49. 这样做是获取你当前 模块控制器 等信息
  50. $request = Request::instance();
  51. echo “当前模块名称是” . $request->module();
  52. echo “当前控制器名称是” . $request->controller();
  53. echo “当前操作名称是” . $request->action();
  54. 获取请求参数
  55. $request = Request::instance();
  56. echo ‘请求方法:’ . $request->method() . ‘<br/>’;
  57. echo ‘资源类型:’ . $request->type() . ‘<br/>’;
  58. echo ‘访问ip地址:’ . $request->ip() . ‘<br/>’;
  59. echo ‘是否AJax请求:’ . var_export($request->isAjax(), true) . ‘<br/>’;
  60. echo ‘请求参数:’;
  61. dump($request->param());
  62. echo ‘请求参数:仅包含name’;
  63. dump($request->only([‘name’]));
  64. echo ‘请求参数:排除name’;
  65. dump($request->except([‘name’]));

原文:http://www.thinkphp.cn/topic/55031.html

  1. 这一部分是请求伪装
  2. // 是否为 GET 请求
  3. if (Request::instance()->isGet()) echo “当前为 GET 请求”;
  4. // 是否为 POST 请求
  5. if (Request::instance()->isPost()) echo “当前为 POST 请求”;
  6. // 是否为 PUT 请求
  7. if (Request::instance()->isPut()) echo “当前为 PUT 请求”;
  8. // 是否为 DELETE 请求
  9. if (Request::instance()->isDelete()) echo “当前为 DELETE 请求”;
  10. // 是否为 Ajax 请求
  11. if (Request::instance()->isAjax()) echo “当前为 Ajax 请求”;
  12. // 是否为 Pjax 请求
  13. if (Request::instance()->isPjax()) echo “当前为 Pjax 请求”;
  14. // 是否为手机访问
  15. if (Request::instance()->isMobile()) echo “当前为手机访问”;
  16. // 是否为 HEAD 请求
  17. if (Request::instance()->isHead()) echo “当前为 HEAD 请求”;
  18. // 是否为 Patch 请求
  19. if (Request::instance()->isPatch()) echo “当前为 PATCH 请求”;
  20. // 是否为 OPTIONS 请求
  21. if (Request::instance()->isOptions()) echo “当前为 OPTIONS 请求”;
  22. // 是否为 cli
  23. if (Request::instance()->isCli()) echo “当前为 cli”;
  24. // 是否为 cgi
  25. if (Request::instance()->isCgi()) echo “当前为 cgi”;
  26. 这一部分是获取当前的信息
  27. $request = Request::instance();
  28. // 获取当前域名
  29. echo domain:  . $request->domain() . ‘<br/>’;
  30. // 获取当前入口文件
  31. echo file:  . $request->baseFile() . ‘<br/>’;
  32. // 获取当前URL地址 不含域名
  33. echo url:  . $request->url() . ‘<br/>’;
  34. // 获取包含域名的完整URL地址
  35. echo url with domain:  . $request->url(true) . ‘<br/>’;
  36. // 获取当前URL地址 不含QUERY_STRING
  37. echo url without query:  . $request->baseUrl() . ‘<br/>’;
  38. // 获取URL访问的ROOT地址
  39. echo root:’ . $request->root() . ‘<br/>’;
  40. // 获取URL访问的ROOT地址
  41. echo root with domain:  . $request->root(true) . ‘<br/>’;
  42. // 获取URL地址中的PATH_INFO信息
  43. echo pathinfo:  . $request->pathinfo() . ‘<br/>’;
  44. // 获取URL地址中的PATH_INFO信息 不含后缀
  45. echo pathinfo:  . $request->path() . ‘<br/>’;
  46. // 获取URL地址中的后缀信息
  47. echo ext:  . $request->ext() . ‘<br/>’;
  48.  
  49. 这样做是获取你当前 模块控制器 等信息
  50. $request = Request::instance();
  51. echo “当前模块名称是” . $request->module();
  52. echo “当前控制器名称是” . $request->controller();
  53. echo “当前操作名称是” . $request->action();
  54. 获取请求参数
  55. $request = Request::instance();
  56. echo ‘请求方法:’ . $request->method() . ‘<br/>’;
  57. echo ‘资源类型:’ . $request->type() . ‘<br/>’;
  58. echo ‘访问ip地址:’ . $request->ip() . ‘<br/>’;
  59. echo ‘是否AJax请求:’ . var_export($request->isAjax(), true) . ‘<br/>’;
  60. echo ‘请求参数:’;
  61. dump($request->param());
  62. echo ‘请求参数:仅包含name’;
  63. dump($request->only([‘name’]));
  64. echo ‘请求参数:排除name’;
  65. dump($request->except([‘name’]));

原文:http://www.thinkphp.cn/topic/55031.html

在控制器中controller
public function school()
{
// 给模板变量name赋值
$id=input(‘get.id’);//页数
$this->assign(‘id’,$id);
//多个参数
$this->assign([
‘name’ =>’thinkphp’,
’email’=>’thinkphp@qq.com’
]);
return $this->fetch();

}

在控制器中controller
public function school()
{
// 给模板变量name赋值
$id=input(‘get.id’);//页数
$this->assign(‘id’,$id);
//多个参数
$this->assign([
‘name’ =>’thinkphp’,
’email’=>’thinkphp@qq.com’
]);
return $this->fetch();

}