Skip to content

Commit 1acdee2

Browse files
committed
updates request url methods.
1 parent 3981352 commit 1acdee2

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"ext-json": "*",
1616
"neuron-php/application": "0.8.*",
1717
"neuron-php/routing": "0.8.*",
18+
"neuron-php/data": "^0.8.6",
1819
"neuron-php/dto": "^0.0.7",
1920
"league/commonmark": "^2.6",
2021
"neuron-php/cli": "0.8.*",

src/Mvc/Controllers/Base.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -778,34 +778,38 @@ protected static function registerDelete( Application $app, string $controller,
778778

779779
/**
780780
* Generate a relative URL for a named route.
781-
*
781+
*
782782
* @param string $routeName The name of the route
783783
* @param array $parameters Parameters to substitute in the route path
784-
* @return string|null The generated relative URL or null if route not found
784+
* @param string|null $fallback Fallback URL if route not found
785+
* @return string|null The generated relative URL, fallback if provided, or null if route not found
785786
*/
786-
protected function urlFor( string $routeName, array $parameters = [] ): ?string
787+
protected function urlFor( string $routeName, array $parameters = [], ?string $fallback = null ): ?string
787788
{
788789
if( !isset( $this->_router ) || !method_exists( $this->_router, 'generateUrl' ) )
789790
{
790-
return null;
791+
return $fallback;
791792
}
792-
return $this->_router->generateUrl( $routeName, $parameters, false );
793+
$url = $this->_router->generateUrl( $routeName, $parameters, false );
794+
return $url ?? $fallback;
793795
}
794796

795797
/**
796798
* Generate an absolute URL for a named route.
797-
*
799+
*
798800
* @param string $routeName The name of the route
799801
* @param array $parameters Parameters to substitute in the route path
800-
* @return string|null The generated absolute URL or null if route not found
802+
* @param string|null $fallback Fallback URL if route not found
803+
* @return string|null The generated absolute URL, fallback if provided, or null if route not found
801804
*/
802-
protected function urlForAbsolute( string $routeName, array $parameters = [] ): ?string
805+
protected function urlForAbsolute( string $routeName, array $parameters = [], ?string $fallback = null ): ?string
803806
{
804807
if( !isset( $this->_router ) || !method_exists( $this->_router, 'generateUrl' ) )
805808
{
806-
return null;
809+
return $fallback;
807810
}
808-
return $this->_router->generateUrl( $routeName, $parameters, true );
811+
$url = $this->_router->generateUrl( $routeName, $parameters, true );
812+
return $url ?? $fallback;
809813
}
810814

811815
/**

src/Mvc/Requests/Request.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,13 @@ class Request
2727
private array $_routeParameters = [];
2828
private array $_errors = [];
2929
private DefaultIpResolver $_ipResolver;
30-
private Cookie $_cookie;
31-
private Get $_get;
32-
private Post $_post;
33-
private Server $_server;
34-
private Session $_session;
3530

3631
/**
3732
* Request constructor.
3833
*/
3934
public function __construct()
4035
{
4136
$this->_ipResolver = new DefaultIpResolver();
42-
$this->_cookie = new Cookie();
43-
$this->_get = new Get();
44-
$this->_post = new Post();
45-
$this->_server = new Server();
46-
$this->_session = new Session();
4737
}
4838

4939
/**
@@ -94,7 +84,7 @@ public function getClientIp(): string
9484
*/
9585
public function get( string $key, mixed $default = null ): mixed
9686
{
97-
return $this->_get->filterScalar( $key, $default );
87+
return Get::filterScalar( $key, $default );
9888
}
9989

10090
/**
@@ -105,7 +95,7 @@ public function get( string $key, mixed $default = null ): mixed
10595
*/
10696
public function post( string $key, mixed $default = null ): mixed
10797
{
108-
return $this->_post->filterScalar( $key, $default );
98+
return Post::filterScalar( $key, $default );
10999
}
110100

111101
/**
@@ -116,7 +106,7 @@ public function post( string $key, mixed $default = null ): mixed
116106
*/
117107
public function server( string $key, mixed $default = null ): mixed
118108
{
119-
return $this->_server->filterScalar( $key, $default );
109+
return Server::filterScalar( $key, $default );
120110
}
121111

122112
/**
@@ -127,7 +117,7 @@ public function server( string $key, mixed $default = null ): mixed
127117
*/
128118
public function session( string $key, mixed $default = null ): mixed
129119
{
130-
return $this->_session->filterScalar( $key, $default );
120+
return Session::filterScalar( $key, $default );
131121
}
132122

133123
/**
@@ -138,7 +128,7 @@ public function session( string $key, mixed $default = null ): mixed
138128
*/
139129
public function cookie( string $key, mixed $default = null ): mixed
140130
{
141-
return $this->_cookie->filterScalar( $key, $default );
131+
return Cookie::filterScalar( $key, $default );
142132
}
143133

144134
/**

versionlog.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
## 0.9.3
1+
## 0.9.5 2025-11-15
2+
* Enhanced `urlFor()` and `urlForAbsolute()` methods to accept optional fallback parameter
3+
* Methods now return fallback URL when route is not found, instead of null
4+
* This eliminates need for null coalescing operator at every call site
5+
6+
## 0.9.4 2025-11-15
7+
* Added missing dependency on neuron-php/data (^0.8.6) which is required by Request class filter methods
28

39
## 0.9.3 2025-11-15
410
* **Breaking Change**: Integrated DTO component for request validation

0 commit comments

Comments
 (0)