We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Router Issues multiple combinations with prefix

I am struggling to work out the desired routes to enable me to use params in all cases where there is a prefix used in my routes. I will work perfectly without the admin prefit but not with routes desired:

/
/action
/action/{id}
/controller/action/
/controller/action/{id]
/admin/
/admin/action
/admin/action/{id}
/admin/controller/action/
/admin/controller/action/{id]

the code below will not allow for:

/admin/action/{id} 

the error is Scorch\Controllers\Admin\EditController handler class cannot be loaded

<?php

$router = $di->getRouter();
$router->removeExtraSlashes(true);

$router->add('/', [
    'namespace' => 'Scorch\Controllers',
    'controller' => 'index'

]);

$router->add('/:controller/:action/:params', [
    'namespace' => 'Scorch\Controllers',
    'controller' => 1,
    'action' => 2,
    'params' => 3,
]);

$router->add('/:controller', [
    'namespace' => 'Scorch\Controllers',
    'controller' => 1
]);

$router->add('/admin', [
    'namespace' => 'Scorch\Controllers\Admin',
    'controller' => 'index'

]);

$router->add('/admin/:controller/:action/:params', [
    'namespace' => 'Scorch\Controllers\Admin',
    'controller' => 1,
    'action' => 2,
    'params' => 3,
]);

$router->add('/admin/:controller', [
    'namespace' => 'Scorch\Controllers\Admin',
    'controller' => 1
]);

$router->handle();

any ideas would be gratefully recieved

Have you tried router groups? I'm using them for similar problem, separate API routes:

// Define new router group
$api = new \Phalcon\Mvc\Router\Group([
    'module' => 'api',
]);
$api->setPrefix('/api/v1');

// API routes
$api->addGet('/news', 'News::index');
$api->addGet('/news/{id:[0-9]+}', 'News::fetch');
$api->addPost('/news', 'News::add');
$api->addPut('/news/{id:[0-9]+}', 'News::edit');
$api->addDelete('/news/{id:[0-9]+}', 'News::delete');

// Add API routes to main router
$router->mount($api);

One way is to make them all the same level /(prefix)/controllers etc

That says it is not finding the class with the namespace "Scorch\Controllers\Admin\EditController" .. does that exist? or set right?

Also.. Modules are nice to add variety as well