Elgg  Version 1.11
Router.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
16 class Router {
17  private $handlers = array();
18  private $hooks;
19 
25  public function __construct(\Elgg\PluginHooksService $hooks) {
26  $this->hooks = $hooks;
27  }
28 
39  public function route(\Elgg\Http\Request $request) {
40  $segments = $request->getUrlSegments();
41  if ($segments) {
42  $identifier = array_shift($segments);
43  } else {
44  $identifier = '';
45 
46  // this plugin hook is deprecated. Use elgg_register_page_handler()
47  // to register for the '' (empty string) handler.
48  // allow plugins to override the front page (return true to indicate
49  // that the front page has been served)
50  $result = _elgg_services()->hooks->trigger('index', 'system', null, false);
51  if ($result === true) {
52  elgg_deprecated_notice("The 'index', 'system' plugin has been deprecated. See elgg_front_page_handler()", 1.9);
53  exit;
54  }
55  }
56 
57  // return false to stop processing the request (because you handled it)
58  // return a new $result array if you want to route the request differently
59  $result = array(
60  'identifier' => $identifier,
61  'handler' => $identifier, // backward compatibility
62  'segments' => $segments,
63  );
64  $result = $this->hooks->trigger('route', $identifier, $result, $result);
65  if ($result === false) {
66  return true;
67  }
68 
69  if ($identifier != $result['identifier']) {
70  $identifier = $result['identifier'];
71  } else if ($identifier != $result['handler']) {
72  $identifier = $result['handler'];
73  }
74 
75  $segments = $result['segments'];
76 
77  $handled = false;
78  if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) {
79  $function = $this->handlers[$identifier];
80  $handled = call_user_func($function, $segments, $identifier);
81  }
82 
83  return $handled || headers_sent();
84  }
85 
95  public function registerPageHandler($identifier, $function) {
96  if (is_callable($function, true)) {
97  $this->handlers[$identifier] = $function;
98  return true;
99  }
100 
101  return false;
102  }
103 
111  public function unregisterPageHandler($identifier) {
112  unset($this->handlers[$identifier]);
113  }
114 
120  public function getPageHandlers() {
121  return $this->handlers;
122  }
123 }
124 
unregisterPageHandler($identifier)
Unregister a page handler for an identifier.
Definition: Router.php:111
__construct(\Elgg\PluginHooksService $hooks)
Constructor.
Definition: Router.php:25
exit
Definition: reorder.php:12
registerPageHandler($identifier, $function)
Register a function that gets called when the first part of a URL is equal to the identifier...
Definition: Router.php:95
Save menu items.
_elgg_services()
Definition: autoloader.php:14
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
route(\Elgg\Http\Request $request)
Routes the request to a registered page handler.
Definition: Router.php:39
getPageHandlers()
Get page handlers as array of identifier => callback.
Definition: Router.php:120
$request