Skip to content

Commit 2925517

Browse files
committed
fixed routes list command.
1 parent e2b256e commit 2925517

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/Mvc/Cli/Commands/Routes/ListCommand.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ private function loadRoutes( string $configPath ): array
144144
return [];
145145
}
146146

147-
foreach( $data as $routePattern => $routeConfig )
147+
// Check if routes are nested under 'routes' key
148+
if( isset( $data['routes'] ) && is_array( $data['routes'] ) )
149+
{
150+
$data = $data['routes'];
151+
}
152+
153+
foreach( $data as $routeName => $routeConfig )
148154
{
149155
// Parse route configuration
150-
$route = $this->parseRoute( $routePattern, $routeConfig );
156+
$route = $this->parseRoute( $routeName, $routeConfig );
151157
if( $route )
152158
{
153159
$routes[] = $route;
@@ -166,29 +172,51 @@ private function loadRoutes( string $configPath ): array
166172
/**
167173
* Parse a single route configuration
168174
*
169-
* @param string $pattern
175+
* @param string $routeName
170176
* @param mixed $config
171177
* @return array|null
172178
*/
173-
private function parseRoute( string $pattern, $config ): ?array
179+
private function parseRoute( string $routeName, $config ): ?array
174180
{
175181
if( !is_array( $config ) )
176182
{
177183
return null;
178184
}
179185

180-
// Extract route information
181-
$controller = $config['controller'] ?? 'Unknown';
182-
$action = $config['method'] ?? $config['action'] ?? 'index';
183-
$httpMethod = strtoupper( $config['type'] ?? $config['http_method'] ?? 'GET' );
186+
// Extract the actual route pattern
187+
$pattern = $config['route'] ?? $routeName;
184188

185-
// Extract parameters if present
189+
// Extract controller and action from controller string (e.g., "App\Controllers\StaticPages@index")
190+
$controllerString = $config['controller'] ?? 'Unknown';
191+
if( strpos( $controllerString, '@' ) !== false )
192+
{
193+
list( $controller, $action ) = explode( '@', $controllerString, 2 );
194+
}
195+
else
196+
{
197+
$controller = $controllerString;
198+
$action = $config['action'] ?? 'index';
199+
}
200+
201+
// Extract HTTP method
202+
$httpMethod = strtoupper( $config['method'] ?? $config['type'] ?? 'GET' );
203+
204+
// Extract parameters from route pattern (e.g., :page, :title)
186205
$parameters = [];
206+
if( preg_match_all( '/:(\w+)/', $pattern, $matches ) )
207+
{
208+
$parameters = $matches[1];
209+
}
210+
211+
// Also check for explicit request parameters
187212
if( isset( $config['request'] ) && is_array( $config['request'] ) )
188213
{
189214
foreach( $config['request'] as $param => $rules )
190215
{
191-
$parameters[] = $param;
216+
if( !in_array( $param, $parameters ) )
217+
{
218+
$parameters[] = $param;
219+
}
192220
}
193221
}
194222

0 commit comments

Comments
 (0)