Blogger Information
Blog 45
fans 0
comment 0
visits 34583
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
处理mvc的控制器访问与参数解析
咸鱼老爷
Original
633 people have browsed it

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

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post