UrlUtils.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * User: HUANGXIANG
  4. * Date: 2017-06-26 16:47
  5. */
  6. namespace App\Utils;
  7. use Request;
  8. class UrlUtils
  9. {
  10. const URL = 'url';
  11. const ACTION = 'action';
  12. const CONTROLLER = 'controller';
  13. const CLASS_NAME = 'class_name';
  14. const CLASS_METHOD = 'class_method';
  15. const METHOD = 'method';
  16. const REAL_METHOD = 'real_method';
  17. public static function toRestfulParams()
  18. {
  19. $params = [];
  20. // 如: App\Http\Controllers\IndexController@getIndex
  21. $action = (Request::route()->getActionName());
  22. // 如: [App\Http\Controllers\IndexController, getIndex]
  23. $tmp = explode("@", $action);
  24. // 如: App\Http\Controllers\IndexController
  25. $controller = $tmp[0];
  26. // 如: getIndex
  27. $classMethod = count($tmp) > 1 ? $tmp[1] : '';
  28. $paths = explode("\\", $controller);
  29. // 如: IndexController
  30. $className = $paths[count($paths) - 1];
  31. // 如: GET | POST | PUT | DELETE , PUT跟DELETE方法可以通过_method参数传
  32. $method = Request::getMethod();
  33. // 如: GET | POST
  34. $realMethod = Request::getRealMethod();
  35. $url = Request::getRequestUri();
  36. $params[static::ACTION] = $action;
  37. $params[static::CONTROLLER] = $controller;
  38. $params[static::CLASS_NAME] = $className;
  39. $params[static::CLASS_METHOD] = $classMethod;
  40. $params[static::METHOD] = $method;
  41. $params[static::REAL_METHOD] = $realMethod;
  42. $params[static::URL] = $url;
  43. return $params;
  44. }
  45. }