处理mvc的控制器访问与参数解析

创建控制器Start.php

  1. <?php
  2. namespace controllers;
  3. class Start
  4. {
  5. public static function int()
  6. {
  7. $pathinfo = array_filter(explode('/', $_SERVER['PATH_INFO']));
  8. //生成控制器
  9. $controller = __NAMESPACE__ . '\\' . ucfirst(array_shift($pathinfo)).'Controller';
  10. $action = array_shift($pathinfo);
  11. //将url中的参数解析出来
  12. $params = [];
  13. for ($i = 0; $i < count($pathinfo); $i += 2) {
  14. if (isset($pathinfo[$i + 1]))
  15. $params[$pathinfo[$i]] = $pathinfo[$i + 1];
  16. }
  17. echo call_user_func_array([(new $controller), $action], $params);
  18. }
  19. }

入口文件index.php

  1. <?php
  2. //入口文件
  3. use controllers\Start;
  4. require __DIR__.'/vendor/autoload.php';
  5. Start::int();

通过pactinfo访问user控制器中的select方法;http://127.0.0.119/pdo/ifram/index.php/user/select
运行结果图

使用curl发起请求

  1. <?php
  2. // https://api.apiopen.top/getJoke?page=1&count=5&type=video
  3. $ustring = 'https://api.apiopen.top/getJoke?';
  4. $page = 1;
  5. $count = 5;
  6. $type = 'video';
  7. $query = http_build_query(['page' => $page, 'count' => $count, 'type' => $type]);
  8. $url = $ustring . $query;
  9. // 发起一个http请求
  10. $api = curl($url);
  11. function curl($url)
  12. {
  13. $ch = curl_init();
  14. curl_setopt($ch, CURLOPT_URL, $url);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  16. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  18. $api = curl_exec($ch);
  19. curl_close($ch);
  20. return $api;
  21. }
  22. ?>
  23. <!DOCTYPE html>
  24. <html lang="zh-CN">
  25. <head>
  26. <meta charset="UTF-8">
  27. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  28. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  29. <script src="//cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
  30. <title>Document</title>
  31. </head>
  32. <body>
  33. <table>
  34. <thead>
  35. <tr>
  36. <th>id</th>
  37. <th>标题</th>
  38. <th>作者</th>
  39. </tr>
  40. </thead>
  41. <tbody>
  42. </tbody>
  43. </table>
  44. </body>
  45. <script>
  46. const api = <?= $api ?>;
  47. let str='';
  48. $.each(api.result,(k,i)=>{
  49. str += '<tr>';
  50. str += '<td>'+i['sid']+'</td>';
  51. str += '<td>'+i['text']+'</td>';
  52. str += '<td>'+i['name']+'</td>';
  53. str +='</tr>';
  54. })
  55. $('tbody').html(str);
  56. </script>
  57. </html>

更多相关文章

  1. 趁火打劫!印度APT组织对我国医疗机构发起定向***
  2. 助力网络基础器件革新:全新单芯片可编程BASE-T PHY /控制器/桥
  3. Alpaca-Spa-Laravel后台管理系统-前后分离
  4. 从入门到放弃,50G编程视频免费送!
  5. MVC控制器类的访问、参数解析、api接口数据获取并渲染
  6. kubernetes其他控制器之PodDisruptionBudget
  7. kubernetes常用控制器之Job和CronJob
  8. kubernetes常用控制器之DaemonSet
  9. kubernetes中其他控制器之PodSecurityPolicy

随机推荐

  1. PHP时间戳和日期格式相互转换
  2. php面向对象之析构函数和对象引用
  3. PHP中面向对象之继承
  4. EasySwoole 基础入门
  5. php实现cookie即时生效
  6. CGI,FastCGI,PHP-CGI,PHP-FPM 简单了解
  7. php session垃圾回收机制
  8. 基于 Carbon 的时间穿梭 Travel 扩展包
  9. PHP面向对象到底是啥?十分钟通俗易懂图文
  10. PHP中的面向对象之构造函数详解