Elgg  Version 4.x
pagehandler.php
Go to the documentation of this file.
1 <?php
27 function elgg_register_route($name, array $params = []) {
28  return _elgg_services()->routes->register($name, $params);
29 }
30 
39  _elgg_services()->routes->unregister($name);
40 }
41 
50 function elgg_get_route(string $name): ?\Elgg\Router\Route {
51  return _elgg_services()->routes->get($name);
52 }
53 
62 function elgg_get_route_for_url(string $url): ?\Elgg\Router\Route {
63  $url = elgg_normalize_url($url);
64 
65  $path = parse_url($url, PHP_URL_PATH);
66  try {
67  $route_info = _elgg_services()->urlMatcher->match($path);
68  if (!isset($route_info['_route'])) {
69  return null;
70  }
71 
72  $route = _elgg_services()->routes->get($route_info['_route']);
73  $route->setMatchedParameters($route_info);
74 
75  return $route;
76  } catch (\Symfony\Component\Routing\Exception\ExceptionInterface $e) {
77  // route matcher exception
78  return null;
79  }
80 }
81 
88 function elgg_get_current_route(): ?\Elgg\Router\Route {
89  return _elgg_services()->request->getRoute();
90 }
91 
98 function elgg_get_current_route_name(): string {
99  $route = _elgg_services()->request->getRoute();
100  return isset($route) ? $route->getName() : '';
101 }
102 
111 function elgg_route_exists(string $name): bool {
112  return _elgg_services()->routes->get($name) instanceof \Elgg\Router\Route;
113 }
114 
123 function elgg_generate_url($name, array $parameters = []) {
124  return _elgg_services()->routes->generateUrl($name, $parameters);
125 }
126 
149 function elgg_generate_entity_url(ElggEntity $entity, $resource = 'view', $subresource = null, array $parameters = []) {
150 
151  $make_route_name = function ($type, $subtype) use ($resource, $subresource) {
152  $route_parts = [
153  $resource,
154  $type,
155  $subtype,
156  $subresource,
157  ];
158 
159  return implode(':', array_filter($route_parts));
160  };
161 
162  $pairs = [
163  [$entity->type, $entity->subtype],
164  [$entity->type, null],
165  ];
166 
167  foreach ($pairs as $pair) {
168  $route_name = $make_route_name($pair[0], $pair[1]);
169  $params = _elgg_services()->routes->resolveRouteParameters($route_name, $entity, $parameters);
170  if ($params !== false) {
171  return elgg_generate_url($route_name, $params);
172  }
173  }
174 
175  return false;
176 }
177 
187 function elgg_generate_action_url($action, array $query = [], $add_csrf_tokens = true) {
188 
189  $url = "action/$action";
192 
193  if ($add_csrf_tokens) {
195  }
196 
197  return $url;
198 }
199 
215 function elgg_ok_response($content = '', $message = '', $forward_url = null, int $status_code = ELGG_HTTP_OK) {
216  if ($message) {
217  if (is_array($message) && !array_key_exists('message', $message)) {
218  elgg_deprecated_notice('Passing an array of messages to ' . __METHOD__ . ' has been deprecated', '4.2');
219 
220  _elgg_services()->system_messages->addSuccessMessage($message);
221  } else {
223  }
224  }
225 
226  return new \Elgg\Http\OkResponse($content, $status_code, $forward_url);
227 }
228 
243  if ($message) {
244  if (is_array($message) && !array_key_exists('message', $message)) {
245  elgg_deprecated_notice('Passing an array of messages to ' . __METHOD__ . ' has been deprecated', '4.2');
246 
247  _elgg_services()->system_messages->addErrorMessage($message);
248  } else {
250 
251  // needed to convert the error message back to a string as the ErrorResponse does not support an array of \ElggSystemMessage options
252  $message = is_array($message) ? $message['message'] : $message;
253  }
254  }
255 
256  return new \Elgg\Http\ErrorResponse($message, $status_code, $forward_url);
257 }
258 
273  return new Elgg\Http\RedirectResponse($forward_url, $status_code);
274 }
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:515
elgg_get_route(string $name)
Get a registered route by it&#39;s name.
Definition: pagehandler.php:50
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(!$entity->delete()) $forward_url
Definition: delete.php:30
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:467
$params
Saves global plugin settings.
Definition: save.php:13
elgg_route_exists(string $name)
Check if a route is registered.
elgg_normalize_url($url)
Definition: output.php:152
elgg_get_current_route_name()
Get the route name for the current request.
Definition: pagehandler.php:98
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: deprecation.php:52
elgg_generate_action_url($action, array $query=[], $add_csrf_tokens=true)
Generate an action URL.
const ELGG_HTTP_OK
Definition: constants.php:60
elgg_unregister_route($name)
Unregister a route by its name.
Definition: pagehandler.php:38
$subtype
Definition: delete.php:22
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:135
$type
Definition: delete.php:21
$resource
$path
Definition: details.php:68
elgg_redirect_response($forward_url=REFERRER, int $status_code=ELGG_HTTP_FOUND)
Prepare a silent redirect response to be returned by a page or an action handler. ...
const REFERRER
Definition: constants.php:42
$entity
Definition: reset.php:8
elgg_generate_url($name, array $parameters=[])
Generate a URL for named route.
const ELGG_HTTP_FOUND
Definition: constants.php:72
elgg_get_route_for_url(string $url)
Find a registered route based on an url/path.
Definition: pagehandler.php:62
elgg_register_route($name, array $params=[])
Elgg page handler functions.
Definition: pagehandler.php:27
elgg_register_error_message($options)
Registers a error system message.
Definition: elgglib.php:62
elgg_get_current_route()
Get the route for the current request.
Definition: pagehandler.php:88
$action
Definition: subscribe.php:11
const ELGG_HTTP_BAD_REQUEST
Definition: constants.php:79
$query
$content
Set robots.txt action.
Definition: set_robots.php:6
elgg_error_response($message= '', $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
elgg_ok_response($content= '', $message= '', $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
elgg_register_success_message($options)
Registers a success system message.
Definition: elgglib.php:43
elgg_generate_entity_url(ElggEntity $entity, $resource= 'view', $subresource=null, array $parameters=[])
Generate entity URL from a named route.