Skip to content

Commit b198497

Browse files
committed
fixes named routes in scaffolding.
1 parent 4fb20fc commit b198497

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

src/Mvc/Cli/Commands/Generate/ControllerCommand.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function configure(): void
6060
public function execute( array $Parameters = [] ): int
6161
{
6262
$this->output->info( "\n╔═══════════════════════════════════════╗" );
63-
$this->output->info( "║ MVC Controller Generator ║" );
63+
$this->output->info( "║ MVC Controller Generator " );
6464
$this->output->info( "╚═══════════════════════════════════════╝\n" );
6565

6666
// Get controller name
@@ -315,34 +315,37 @@ private function generateRoutes( array $info ): bool
315315
}
316316
$controller = $namespace . '\\' . $info['class'];
317317

318-
// Build routes
318+
// Generate route name prefix (e.g., "posts" or "admin_posts")
319+
$routeNamePrefix = strtolower( str_replace( '/', '_', $info['controllerPath'] ) );
320+
321+
// Build routes with named keys
319322
$newRoutes = [
320-
[
323+
$routeNamePrefix . '_index' => [
321324
'method' => 'GET',
322325
'route' => $info['routePrefix'],
323326
'controller' => $controller . '@index',
324327
],
325-
[
328+
$routeNamePrefix . '_create' => [
326329
'method' => 'GET',
327330
'route' => $info['routePrefix'] . '/create',
328331
'controller' => $controller . '@create',
329332
],
330-
[
333+
$routeNamePrefix . '_store' => [
331334
'method' => 'POST',
332335
'route' => $info['routePrefix'],
333336
'controller' => $controller . '@store',
334337
],
335-
[
338+
$routeNamePrefix . '_edit' => [
336339
'method' => 'GET',
337340
'route' => $info['routePrefix'] . '/:id/edit',
338341
'controller' => $controller . '@edit',
339342
],
340-
[
343+
$routeNamePrefix . '_update' => [
341344
'method' => 'PUT',
342345
'route' => $info['routePrefix'] . '/:id',
343346
'controller' => $controller . '@update',
344347
],
345-
[
348+
$routeNamePrefix . '_destroy' => [
346349
'method' => 'DELETE',
347350
'route' => $info['routePrefix'] . '/:id',
348351
'controller' => $controller . '@destroy',
@@ -352,11 +355,28 @@ private function generateRoutes( array $info ): bool
352355
// Add show route for API controllers
353356
if( $info['isApi'] )
354357
{
355-
array_splice( $newRoutes, 1, 0, [[
356-
'method' => 'GET',
357-
'route' => $info['routePrefix'] . '/:id',
358-
'controller' => $controller . '@show',
359-
]]);
358+
// Insert show route after index, before create
359+
$showRoute = [
360+
$routeNamePrefix . '_show' => [
361+
'method' => 'GET',
362+
'route' => $info['routePrefix'] . '/:id',
363+
'controller' => $controller . '@show',
364+
]
365+
];
366+
367+
// Merge in the correct position
368+
$routesArray = [];
369+
$inserted = false;
370+
foreach( $newRoutes as $key => $route )
371+
{
372+
$routesArray[$key] = $route;
373+
if( $key === $routeNamePrefix . '_index' && !$inserted )
374+
{
375+
$routesArray = array_merge( $routesArray, $showRoute );
376+
$inserted = true;
377+
}
378+
}
379+
$newRoutes = $routesArray;
360380
}
361381

362382
// Add filter if specified

src/Mvc/Cli/Commands/Generate/EventCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function configure(): void
5151
public function execute(): int
5252
{
5353
$this->output->info( "\n╔═══════════════════════════════════════╗" );
54-
$this->output->info( "║ Event Generator ║" );
54+
$this->output->info( "║ Event Generator " );
5555
$this->output->info( "╚═══════════════════════════════════════╝\n" );
5656

5757
// Get event name

src/Mvc/Cli/Commands/Generate/JobCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function configure(): void
5454
public function execute(): int
5555
{
5656
$this->output->info( "\n╔═══════════════════════════════════════╗" );
57-
$this->output->info( "║ Job Generator ║" );
57+
$this->output->info( "║ Job Generator " );
5858
$this->output->info( "╚═══════════════════════════════════════╝\n" );
5959

6060
// Get job name

src/Mvc/Cli/Commands/Generate/ListenerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function configure(): void
5353
public function execute(): int
5454
{
5555
$this->output->info( "\n╔═══════════════════════════════════════╗" );
56-
$this->output->info( "║ Listener Generator ║" );
56+
$this->output->info( "║ Listener Generator " );
5757
$this->output->info( "╚═══════════════════════════════════════╝\n" );
5858

5959
// Get listener name

versionlog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## 0.8.9
2+
* Fixed named routes in controller scaffolding.
23

34
## 0.8.8 2025-11-11
45
* Fixed named routes.

0 commit comments

Comments
 (0)