You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.1KB

  1. <?php
  2. use Pecee\SimpleRouter\SimpleRouter as Router;
  3. use Pecee\Http\Url;
  4. use Pecee\Http\Response;
  5. use Pecee\Http\Request;
  6. /**
  7. * Get url for a route by using either name/alias, class or method name.
  8. *
  9. * The name parameter supports the following values:
  10. * - Route name
  11. * - Controller/resource name (with or without method)
  12. * - Controller class name
  13. *
  14. * When searching for controller/resource by name, you can use this syntax "route.name@method".
  15. * You can also use the same syntax when searching for a specific controller-class "MyController@home".
  16. * If no arguments is specified, it will return the url for the current loaded route.
  17. *
  18. * @param string|null $name
  19. * @param string|array|null $parameters
  20. * @param array|null $getParams
  21. * @return \Pecee\Http\Url
  22. * @throws \InvalidArgumentException
  23. */
  24. function url(?string $name = null, $parameters = null, ?array $getParams = null): Url
  25. {
  26. return Router::getUrl($name, $parameters, $getParams);
  27. }
  28. /**
  29. * @return \Pecee\Http\Response
  30. */
  31. function response(): Response
  32. {
  33. return Router::response();
  34. }
  35. /**
  36. * @return \Pecee\Http\Request
  37. */
  38. function request(): Request
  39. {
  40. return Router::request();
  41. }
  42. /**
  43. * Get input class
  44. * @param string|null $index Parameter index name
  45. * @param string|null $defaultValue Default return value
  46. * @param array ...$methods Default methods
  47. * @return \Pecee\Http\Input\InputHandler|array|string|null
  48. */
  49. function input($index = null, $defaultValue = null, ...$methods)
  50. {
  51. if ($index !== null) {
  52. return request()->getInputHandler()->value($index, $defaultValue, ...$methods);
  53. }
  54. return request()->getInputHandler();
  55. }
  56. /**
  57. * @param string $url
  58. * @param int|null $code
  59. */
  60. function redirect(string $url, ?int $code = null): void
  61. {
  62. if ($code !== null) {
  63. response()->httpCode($code);
  64. }
  65. response()->redirect($url);
  66. }
  67. /**
  68. * Get current csrf-token
  69. * @return string|null
  70. */
  71. function csrf_token(): ?string
  72. {
  73. $baseVerifier = Router::router()->getCsrfVerifier();
  74. if ($baseVerifier !== null) {
  75. return $baseVerifier->getTokenProvider()->getToken();
  76. }
  77. return null;
  78. }