Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

62 řádky
1.5KB

  1. <?php
  2. namespace App\Util;
  3. use App\Codes\UserRole;
  4. use Illuminate\Support\Facades\Route;
  5. use Illuminate\Support\Str;
  6. class RouteHelper
  7. {
  8. const ENTRY = 'entry';
  9. static public function get(string $url, string $class)
  10. {
  11. return Route::get($url, [$class, self::ENTRY])->name(self::routeName($class));
  12. }
  13. static public function post(string $url, string $class)
  14. {
  15. return Route::post($url, [$class, self::ENTRY])->name(self::routeName($class));
  16. }
  17. static public function server(string $url, string $class)
  18. {
  19. return Route::post($url, [$class, self::ENTRY])->name(self::routeName($class));
  20. }
  21. static public function routeName(string $class)
  22. {
  23. $ele = explode('\\', $class);
  24. $controllerName = array_pop($ele);
  25. $groupName = array_pop($ele);
  26. $routeName = Str::replaceLast('Controller', '', $groupName . $controllerName);
  27. return $routeName;
  28. }
  29. static public function getPath(string $controllerClassName, array $param = [])
  30. {
  31. return route(self::routeName($controllerClassName), $param);
  32. }
  33. static public function webRoute(string $route)
  34. {
  35. return Str::replaceFirst('/api', '', $route);
  36. }
  37. /**
  38. * @param UserRole[] $roles
  39. */
  40. static public function role(array $roles): string
  41. {
  42. $rolesStrArr = [];
  43. foreach ($roles as $role) {
  44. $rolesStrArr[] = $role->value;
  45. }
  46. $ret = "role:" . implode("-", $rolesStrArr);
  47. return $ret;
  48. }
  49. }