Route.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 插件路由
  13. // +----------------------------------------------------------------------
  14. namespace think\addons;
  15. use think\App;
  16. use think\exception\HttpException;
  17. use think\facade\Event;
  18. use think\facade\Request;
  19. class Route
  20. {
  21. public static function execute($addon = null, $controller = null, $action = null)
  22. {
  23. $request = \request();
  24. // 是否自动转换控制器和操作名
  25. $convert = true;
  26. $filter = $convert ? 'strtolower' : 'trim';
  27. $addon = $addon ? trim(call_user_func($filter, $addon)) : '';
  28. $controller = $controller ? trim(call_user_func($filter, $controller)) : 'index';
  29. $action = $action ? trim(call_user_func($filter, $action)) : 'index';
  30. Event::trigger('addon_begin', $request);
  31. if (!empty($addon) && !empty($controller) && !empty($action)) {
  32. $info = get_addon_info($addon);
  33. if (!$info) {
  34. throw new HttpException(404, 'addon %s not found');
  35. }
  36. if (!$info['status']) {
  37. throw new HttpException(500, 'addon %s is disabled');
  38. }
  39. $path = app()->request->root();
  40. if ("" !== $path) {
  41. throw new HttpException(404, 'addon %s not found');
  42. }
  43. app()->http->setBind();
  44. // 设置当前请求的控制器、操作
  45. $request->setController($controller)->setAction($action);
  46. // 监听addon_module_init
  47. Event::trigger('addon_module_init', $request);
  48. $class = get_addon_class($addon, 'controller', $controller);
  49. if (!$class) {
  50. throw new HttpException(404, 'addon controller %s not found', $controller);
  51. }
  52. $instance = new $class(app());
  53. $vars = [];
  54. if (is_callable([$instance, $action])) {
  55. // 执行操作方法
  56. $call = [$instance, $action];
  57. } elseif (is_callable([$instance, '_empty'])) {
  58. // 空操作
  59. $call = [$instance, '_empty'];
  60. $vars = [$action];
  61. } else {
  62. // 操作不存在
  63. throw new HttpException(404, 'addon action %s not found', get_class($instance) . '->' . $action . '()');
  64. }
  65. Event::trigger('addon_action_begin', $call);
  66. return call_user_func_array($call, $vars);
  67. } else {
  68. abort(500, 'addon can not be empty');
  69. }
  70. }
  71. }