/** * Builds and returns the HTML needed to fill a tab to display * within the Debug Bar * * @return string */ public function display() : string { $routes = Services::routes(true); $router = Services::router(null, true); $output = "<h3>Matched Route</h3>"; $output .= "<table><tbody>"; if ($match = $router->getMatchedRoute()) { $output .= "<tr><td>{$match[0]}</td>"; $output .= "<td>{$match[1]}</td></tr>"; } $output .= "<tr><td>Directory:</td><td>" . htmlspecialchars($router->directory()) . "</td></tr>"; $output .= "<tr><td>Controller:</td><td>" . htmlspecialchars($router->controllerName()) . "</td></tr>"; $output .= "<tr><td>Method:</td><td>" . htmlspecialchars($router->methodName()) . "</td></tr>"; $output .= "<tr><td>Params:</td><td>" . print_r($router->params(), true) . "</td></tr>"; $output .= "</table></tbody>"; $output .= "<h3>Defined Routes</h3>"; $output .= "<table><tbody>"; $routes = $routes->getRoutes(); foreach ($routes as $from => $to) { $output .= "<tr>"; $output .= "<td>" . htmlspecialchars($from) . "</td>"; $output .= "<td>" . htmlspecialchars($to) . "</td>"; $output .= "</tr>"; } $output .= "</tbody></table>"; return $output; }
public function testRun404Override() { $_SERVER['argv'] = ['index.php', '/']; $_SERVER['argc'] = 2; // Inject mock router. $routes = Services::routes(); $routes->setAutoRoute(false); $routes->set404Override('Home::index'); $router = Services::router($routes); Services::injectMock('router', $router); ob_start(); $this->codeigniter->run(); $output = ob_get_clean(); $this->assertContains('<h1>Welcome to CodeIgniter</h1>', $output); }
/** * Convenience method that works with the current global $request and * $router instances to redirect using named/reverse-routed routes * to determine the URL to go to. If nothing is found, will treat * as a traditional redirect and pass the string in, letting * $response->redirect() determine the correct method and code. * * If more control is needed, you must use $response->redirect explicitly. * * @param string $uri * @param $params */ function redirect(string $uri, ...$params) { $response = Services::response(null, true); $routes = Services::routes(true); if ($route = $routes->reverseRoute($uri, ...$params)) { $uri = $route; } $response->redirect($uri); }