Elgg  Version 5.1
pagehandler.php
Go to the documentation of this file.
1 <?php
27 function elgg_register_route(string $name, array $params = []): \Elgg\Router\Route {
28  return _elgg_services()->routes->register($name, $params);
29 }
30 
38 function elgg_unregister_route(string $name): void {
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 
121 function elgg_get_current_url(): string {
122  return _elgg_services()->request->getCurrentURL();
123 }
124 
133 function elgg_generate_url(string $name, array $parameters = []): ?string {
134  return _elgg_services()->routes->generateUrl($name, $parameters);
135 }
136 
159 function elgg_generate_entity_url(ElggEntity $entity, string $resource = 'view', string $subresource = null, array $parameters = []): ?string {
160 
161  $make_route_name = function ($type, $subtype) use ($resource, $subresource) {
162  $route_parts = [
163  $resource,
164  $type,
165  $subtype,
166  $subresource,
167  ];
168 
169  return implode(':', array_filter($route_parts));
170  };
171 
172  $pairs = [
173  [$entity->type, $entity->subtype],
174  [$entity->type, null],
175  ];
176 
177  foreach ($pairs as $pair) {
178  $route_name = $make_route_name($pair[0], $pair[1]);
179  $params = _elgg_services()->routes->resolveRouteParameters($route_name, $entity, $parameters);
180  if ($params !== false) {
181  return elgg_generate_url($route_name, $params);
182  }
183  }
184 
185  return null;
186 }
187 
197 function elgg_generate_action_url(string $action, array $query = [], bool $add_csrf_tokens = true): string {
198 
199  $url = elgg_http_add_url_query_elements("action/{$action}", $query);
201 
202  if ($add_csrf_tokens) {
204  }
205 
206  return $url;
207 }
208 
224 function elgg_ok_response($content = '', string|array $message = '', string $forward_url = null, int $status_code = ELGG_HTTP_OK): \Elgg\Http\OkResponse {
225  if ($message) {
227  }
228 
229  return new \Elgg\Http\OkResponse($content, $status_code, $forward_url);
230 }
231 
245 function elgg_error_response(string|array $message = '', string $forward_url = REFERRER, int $status_code = ELGG_HTTP_BAD_REQUEST): \Elgg\Http\ErrorResponse {
246  if ($message) {
248 
249  // needed to convert the error message back to a string as the ErrorResponse does not support an array of \ElggSystemMessage options
250  $message = is_array($message) ? $message['message'] : $message;
251  }
252 
253  return new \Elgg\Http\ErrorResponse($message, $status_code, $forward_url);
254 }
255 
269 function elgg_redirect_response(string $forward_url = REFERRER, int $status_code = ELGG_HTTP_FOUND): \Elgg\Http\RedirectResponse {
270  return new Elgg\Http\RedirectResponse($forward_url, $status_code);
271 }
272 
284 function elgg_download_response(string $content, string $filename = '', bool $inline = false, array $headers = []): \Elgg\Http\DownloadResponse {
285  $response = new \Elgg\Http\DownloadResponse($content);
286  $response->setHeaders($headers);
287  $response->setFilename($filename, $inline);
288 
289  return $response;
290 }
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
elgg_get_route(string $name)
Get a registered route by it&#39;s name.
Definition: pagehandler.php:50
$params
Saves global plugin settings.
Definition: save.php:13
elgg_route_exists(string $name)
Check if a route is registered.
elgg_register_route(string $name, array $params=[])
Elgg page handler functions.
Definition: pagehandler.php:27
elgg_get_current_route_name()
Get the route name for the current request.
Definition: pagehandler.php:98
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_ok_response($content= '', string|array $message= '', string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
$response
Definition: content.php:10
elgg_register_success_message(string|array $options)
Registers a success system message.
Definition: elgglib.php:43
const ELGG_HTTP_OK
Definition: constants.php:45
elgg_add_action_tokens_to_url(string $url, bool $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:152
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
$inline
Definition: save.php:22
elgg_unregister_route(string $name)
Unregister a route by its name.
Definition: pagehandler.php:38
$type
Definition: delete.php:22
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:139
$resource
$path
Definition: details.php:70
elgg_download_response(string $content, string $filename= '', bool $inline=false, array $headers=[])
Prepare a download response.
elgg_error_response(string|array $message= '', string $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page...
Definition: constants.php:37
$entity
Definition: reset.php:8
elgg_redirect_response(string $forward_url=REFERRER, int $status_code=ELGG_HTTP_FOUND)
Prepare a silent redirect response to be returned by a page or an action handler. ...
elgg_generate_entity_url(ElggEntity $entity, string $resource= 'view', string $subresource=null, array $parameters=[])
Generate entity URL from a named route.
elgg_get_current_url()
Returns the current page&#39;s complete URL.
const ELGG_HTTP_FOUND
Definition: constants.php:57
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:181
if(!$entity->delete()) $forward_url
Definition: delete.php:31
elgg_get_route_for_url(string $url)
Find a registered route based on an url/path.
Definition: pagehandler.php:62
elgg_register_error_message(string|array $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:64
$subtype
Definition: delete.php:23
$query
$content
Set robots.txt action.
Definition: set_robots.php:6
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
elgg_normalize_url(string $url)
Definition: output.php:163