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