Sort controllers in subfolders [best practice]


#1

Hello guys,

thx for your work I’m appreciating it!

What is the best practice for using the same controller-names for different purposes?

I would like to create two controllers with the same name. One for admin purposes and the other one for the normal visitors.

My first thought was to create the admin controllers within a subfolder named ‘admin’.

I have no idea whats the next - best practice - step.

Is there any build-in possiblity to achieve that?

Thanks in advance

Phil


#2

Did the trick with the Router::prefix() method.


#3

use namespace to sparate controller

example for build-in possiblity, use this command in your command line

bin/cake bake controller users --prefix admin

it will create UsersController inside Admin folder (namespace);

and to make it accessible define route prefix in your route file;

Router::prefix(‘admin’, function (RouteBuilder $routes) {
$routes->connect(’/’, [‘controller’ => ‘Users’, ‘action’ => ‘index’]);
});

Route official documentation
https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing

hope it help :slight_smile: