Skip to content

Commit

Permalink
update file
Browse files Browse the repository at this point in the history
  • Loading branch information
juancristobalgd1 authored Mar 5, 2024
1 parent 562eeb2 commit 6788390
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/libraries/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,13 @@ public function getAllRoutes(): array
}

/**
* Gets the callback of a route.
* isset route.
*
* @param string $method
* @return array
*/
public function getRouteMap(string $method): array
public function isRoute(string $routeName, string $method = 'GET'): bool
{
return static::$routeMap[$method] ?? [];
return isset(static::$routeMap[$method][$routeName]);
}

/**
Expand Down Expand Up @@ -228,8 +227,7 @@ public static function middleware($middleware)
}

/**
* Allows defining groups/prefixes of routes with shared
* properties (middleware, namespace, etc).
* Allows defining groups/prefixes of routes with shared properties (middleware, namespace, etc).
* @param string $prefix
* @param callable $callback
*/
Expand Down Expand Up @@ -258,8 +256,7 @@ public static function view(string $route, string $view, array $params = [])
}

/**
* Dispatches the request, resolving the route callback
* or handling unregistered routes.
* Dispatches the request, resolving the route callback or handling unregistered routes.
*/
public function dispatch()
{
Expand Down Expand Up @@ -314,10 +311,7 @@ private function resolve($callback)
}

/**
* Handles unregistered route cases, sending a 404 response in production or
* throwing an exception in development.
*
* @return mixed Sends a 404 response or throws a RuntimeException.
* Handles unregistered route cases, sending a 404 response in production or throwing an exception in development.
* @throws RuntimeException When no route is registered, and the application is not in production.
*/
private function notRouteRegistered()
Expand Down Expand Up @@ -399,7 +393,6 @@ private function invokeRouteWithSingleCallback(string $callback)

$instance = $this->createInstance($callback);
if (method_exists($instance, 'index')) {

return $instance->response()->send($instance->index());
}

Expand Down Expand Up @@ -477,23 +470,47 @@ private function compileRoute(string $route)
}

/**
* Generates the URL associated with the path name.
* Generates the URL associated with the route name.
*
* @param string $routeName
* @param array $params
* @return string|null
*/
public static function url(string $routeName, array $params = null)
public function route(string $routeName, array $params = null)
{
if ($route = static::$routeMap['GET'][$routeName] ?? null) {
foreach ($params as $key => $value) {
foreach ($params ?? [] as $key => $value) {
$route = str_replace("{$key}", $value, $route);
}
return $route;
}
return null;
}

/**
* Generates the URL associated with the path name.
*
* @param string $routeName
* @return string|null
*/
public function url(string $routeName)
{
return $this->baseUrl($routeName);
}

/**
* Returns the full site root.
**/
public function baseUrl(string $path = '/'): string
{
$scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http';
$scriptPath = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
$baseUrl = "{$scheme}://{$_SERVER['HTTP_HOST']}{$scriptPath}";
$path = trim($this->prefix . '/' . ltrim($path, '/'), '/');

return "{$baseUrl}/{$path}";
}

/**
* Renders the error view based on the specified HTTP error code.
* @return mixed The result of rendering the error view.
Expand Down

0 comments on commit 6788390

Please sign in to comment.