Get the currently dispatched route instance.
public static current ( ) : Illuminate\Routing\Route | ||
return | Illuminate\Routing\Route |
$routeCollector = new RouteCollector(new StdRouteParser(), new GroupCountBased()); $routeCollector->addRoute('GET', '/users', 'userController@index'); $routeCollector->addRoute('GET', '/users/{id:\d+}', 'userController@show'); $routeCollector->addRoute('POST', '/users', 'userController@store'); $routeCollector->addRoute('PUT', '/users/{id:\d+}', 'userController@update'); $routeCollector->addRoute('DELETE', '/users/{id:\d+}', 'userController@destroy'); $dispatcher = new RouteDispatcher($routeCollector->getData()); $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $routeInfo = $dispatcher->dispatch($httpMethod, $uri); if ($routeInfo[0] === Dispatcher::FOUND) { $handler = $routeInfo[1]; $vars = $routeInfo[2]; [$controller, $method] = explode('@', $handler); // perform action based on current route }In this example, the code sets up a route collector and defines some routes. Then, it creates a dispatcher object using the route collector data and dispatches the current request to determine the active route. If a route is found, the code extracts the handler and variables, and performs actions based on the current route. The FastRoute package is used to provide the current route functionality.
public static current ( ) : Illuminate\Routing\Route | ||
return | Illuminate\Routing\Route |