Trying to change /controller/action/ from old url to a different url

I’m working on this project wherein I wanted to change the URL of an existing /controller/action/ generated by bake.

I have a ForumThreadsController and a ForumCategoriesController.

From my forum-threads index.ctp page, I have created a dropdown filter so I can sort my categories.

However, upon selecting a certain category, I will be directed to this URL: project.com/forum-categories/view/<selected_id>

I want to change this URL to project.com/forum-threads?forum_category_id=<selected_id>

I have already been working with config/routes.php and .htaccess.

Here is my routes.php:

Router::defaultRouteClass(DashedRoute::class);

Router::scope(‘/’, function (RouteBuilder $routes) {
$routes->connect(‘/’, [‘controller’ => ‘home’, ‘action’ => ‘index’]);
$routes->connect(‘/support/*’, [‘controller’ => ‘support’, ‘action’ => ‘display’]);
$routes->fallbacks(DashedRoute::class);
});

Plugin::routes();

And here is my .htaccess:

RewriteEngine on RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L]

Nothing seems to work for me yet.
Thanks in advance for your help. :sweat_smile:

Why not change your ForumThreadsController::index function to

public function index($forum_category_id = null) {

and call this action instead of ForumCategories::view?

this way, you would have the URL you want…

Where can I change ForumCategories::view to ForumThreads::index?

Thanks. :grinning:

That is just a link, isn’t it?

Somewhere in your code you have something like

$this->Html->link(__(‘Show Threads’), [‘controller’ => ‘ForumCategories’, ‘action’ => ‘view’, $id]

and can change it to

$this->html->link(__(‘Show Threads’), [‘controller’ => ‘ForumThreads’, ‘action’ => ‘index’, ‘?’ => [‘forum_category_id’ => $id]])

This will call the ForumThreadsController’s index function and pass the forum_category_id as GET/Query-parameter…

1 Like