Elgg\Router::route PHP Method

route() public method

This function triggers a plugin hook 'route', $identifier so that plugins can modify the routing or handle a request.
public route ( Request $request ) : boolean
$request Elgg\Http\Request The request to handle.
return boolean Whether the request was routed successfully.
    public function route(Request $request)
    {
        $segments = $request->getUrlSegments();
        if ($segments) {
            $identifier = array_shift($segments);
        } else {
            $identifier = '';
        }
        // return false to stop processing the request (because you handled it)
        // return a new $result array if you want to route the request differently
        $old = array('identifier' => $identifier, 'handler' => $identifier, 'segments' => $segments);
        if ($this->timer) {
            $this->timer->begin(['build page']);
        }
        ob_start();
        $result = $this->hooks->trigger('route', $identifier, $old, $old);
        if ($result === false) {
            $output = ob_get_clean();
            $response = elgg_ok_response($output);
        } else {
            if ($result !== $old) {
                _elgg_services()->logger->warn('Use the route:rewrite hook to modify routes.');
            }
            if ($identifier != $result['identifier']) {
                $identifier = $result['identifier'];
            } else {
                if ($identifier != $result['handler']) {
                    $identifier = $result['handler'];
                }
            }
            $segments = $result['segments'];
            $response = false;
            if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) {
                $function = $this->handlers[$identifier];
                $response = call_user_func($function, $segments, $identifier);
            }
            $output = ob_get_clean();
            if ($response === false) {
                return headers_sent();
            }
            if (!$response instanceof ResponseBuilder) {
                $response = elgg_ok_response($output);
            }
        }
        if (_elgg_services()->responseFactory->getSentResponse()) {
            return true;
        }
        _elgg_services()->responseFactory->respond($response);
        return headers_sent();
    }

Usage Example

Example #1
0
 /**
  * 1. Register a page handler for `/foo`
  * 2. Register a plugin hook that uses the "handler" result param
  *    to route all `/bar/*` requests to the `/foo` handler.
  * 3. Route a request for a `/bar` page.
  * 4. Check that the `/foo` handler was called.
  */
 function testRouteSupportsSettingHandlerInHookResultForBackwardsCompatibility()
 {
     $this->router->registerPageHandler('foo', array($this, 'foo_page_handler'));
     $this->hooks->registerHandler('route', 'bar', array($this, 'bar_route_handler'));
     $query = http_build_query(array('__elgg_uri' => 'bar/baz'));
     ob_start();
     $this->router->route(\Elgg\Http\Request::create("http://localhost/?{$query}"));
     ob_end_clean();
     $this->assertEquals(1, $this->fooHandlerCalls);
 }
All Usage Examples Of Elgg\Router::route