Group Controllers With URL

So I am working on an application which will have three different interfaces: a store, an admin page and a user page.

Authorisation is handled externally, based on URLs. So /my_app/admin could be restricted to admins, /my_app/store to the tablet in the shop and /my_app/users to anyone properly logged in.

With CakePHP these urls could be realised by having only three controllers with those names. However, the admin page for instance will have many models to work with, so having all the actions for different models in one controller is not convenient at all. So, the question:

Is it possible to ‘group’ controllers with a URL? e.g. /my_app/group_name/controller1/action1/argument and /my_app/group_name/controller2/action5/argument.

With Routing you can design URLs in pretty much any way you like.

So you don’t necessarilly need three controllers to display those URLs.

You can easily do things like:

$routes->connect('/user-group/entries/*', ['controller' => 'Users', 'action' => 'entries']);

$routes->connect('/store/*', ['controller' => 'Articles', 'action' => 'index']);

$routes->connect('/store/items/*', ['controller' => 'Users', 'action' => 'items']);

$routes->connect('/users/*', ['controller' => 'Categories', 'action' => 'list_users']);

The controller/action -type URLs which CakePHP creates by default can and should be overwritten to better fit your app, you definitely shouldn’t try to fit your controller methods in that structure.

Also, you might be interested in prefix routing.

Prefix Routing would be the best solution for this.

I got it! Thank you very much!

Prefix routing is indeed the way to go, it accomplishes exactly what I was looking for.

I initially refrained from using routes, because it seems it only add extra links. e.g. connection “/admin/*” with “/products/index” would only add a shortcut, it does not block direct usage of “/products/index”,right?

That’s right, but do you need the default URL’s to be blocked?

Normally, no. It wouldn’t be an issue.

However, in the scenario I described in the first post the URL will form the bases of the authorisation system that we use. So via the routed URL someone would be allowed, but not via the unaltered URL.

(Just to be clear, prefixed routing does not have this issue and my questions has been answered.)

Oh, right, since you’re authenticating externally.