Skip to content

Commit

Permalink
Add server as fallback after request and query on get()
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Apr 17, 2023
1 parent ee99b46 commit 80dd2fa
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Router::configure(static function (Routes $routes, MappingInterfaces $mappingInt
$routes->redirect('docs', 'https://gacela-project.com/');

# http://localhost:8081?number=456
$routes->get('/', CustomController::class);
$routes->match(['GET', 'POST'], '/', CustomController::class);

# http://localhost:8081/custom/123
$routes->get('custom/{number}', CustomControllerWithDependencies::class, 'customAction');
Expand Down
7 changes: 4 additions & 3 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public function __construct(
public function __invoke(): string
{
$number = $this->request->get('number');
$method = $this->request->get('REQUEST_METHOD');

if (!empty($number)) {
return "__invoke with GET 'number'={$number}";
return sprintf("__invoke with %s 'number'=%d", $method, $number);
}

return '__invoke';
return '__invoke with ' . $method;
}

public function customAction(int $number = 0): string
Expand All @@ -40,7 +41,7 @@ public function customAction(int $number = 0): string
$routes->redirect('docs', 'https://gacela-project.com/');

# Try it out: http://localhost:8081?number=456
$routes->get('/', Controller::class);
$routes->match(['GET', 'POST'], '/', Controller::class);

# Try it out: http://localhost:8081/custom/123
$routes->get('custom/{number}', Controller::class, 'customAction');
Expand Down
2 changes: 2 additions & 0 deletions src/Router/Entities/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class Request
public const METHOD_POST = 'POST';
public const METHOD_PUT = 'PUT';
public const METHOD_TRACE = 'TRACE';

public const ALL_METHODS = [
self::METHOD_CONNECT,
self::METHOD_DELETE,
Expand Down Expand Up @@ -58,6 +59,7 @@ public function get(string $key): mixed
{
return $this->request[$key]
?? $this->query[$key]
?? $this->server[$key]
?? null;
}
}

0 comments on commit 80dd2fa

Please sign in to comment.