博主信息
博文 45
粉丝 0
评论 0
访问量 46941
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
处理mvc的控制器访问与参数解析
咸鱼老爷
原创
849人浏览过

处理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>

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学