Hi, I needed to change the path for an entity and thought I could just make a custom route rule without renaming the original database table.
So I added the ‘ExternalItems’ line to my routes.php:
$routes->scope('/', function (RouteBuilder $builder): void {
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
/*
* ...and connect the rest of 'Pages' controller's URLs.
*/
$builder->connect('/pages/*', 'Pages::display');
$builder->resources('ExternalItems', ['path' => 'external-references']);
/*
* Connect catchall routes for all controllers.
*
* The `fallbacks` method is a shortcut for
*
* ```
* $builder->connect('/{controller}', ['action' => 'index']);
* $builder->connect('/{controller}/{action}/*', []);
* ```
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$builder->fallbacks();
});
Now, if I use the HTML helper for making a link, with action=‘view’, I get a link with the new url, external-references
. However, an HTML helper link with action=‘edit’ generates a link with the old path in the url, external-items
.
Examples:
<?= $this->Html->link(__('View External Reference'), ['controller' => 'ExternalItems', 'action' => 'view', $externalItem->id], ['class' => 'btn btn-sm btn-outline-dark edit-entity-button']) ?>
<?= $this->Html->link(__('Edit External Reference'), ['controller' => 'ExternalItems', 'action' => 'edit', $externalItem->id], ['class' => 'btn btn-sm btn-primary edit-entity-button']) ?>
The first one respects the new route, the second one doesn’t. Am I missing a trick somewhere?
Thanks.
EDIT: I see now that urls with the new path and an action don’t work. E.g. mywebsite/external-references/edit/999
looks for the non existent controller ExternalReferences.
Though mywebsite/external-references/999
works for a view and the index page works: mywebsite/external-references
.
I guess I need to add more comprehensive rules to the router which cover these use cases?