CakePHP 3 routing prefix without subdirectory/subnamespace

I looking-for solution for using CakePHP 3 routing prefix/scope for using one controller in src/Controllers for action with and without prefix.

/admin/users/index and /users/index both works with src/Controller/UsersController

In beforeFilter I will be check when using prefix.

Standard prefixes won’t really work for this. Instead you could use multiple routes pointing at the same controller/actions. However, unless there are additional parameters to distinguish the routes you’ll have a hard time generating /admin URLs.

Router::scope('/', function ($routes) {
  $routes->connect('/users/:action', ['controller' => 'Users']);

// You can't use 'prefix' as that means something in CakePHP
Router::scope('/admin', ['admin' => true], function ($routes) {
  $routes->connect('/users/:action', ['controller' => 'Users']);

That should work, and will let you generate reverse routes by including admin => true in your route arrays.

That’s work, but when i create simple link

echo $this->Html->link('Test admin', [
	'action' => 'test'

The admin is not included, but should be by default (when I’m on URL with admin

Custom parameters don’t automatically persist, or apply to generated URLs. You’ll have to manually pass them in, or use persistent parameters.