0
votes

I have a plugin with the name 'Admin' and my methods are like index, add etc. Since I want to apply unique model names and controller names (for ACL implementation), I prefixed all controllers and models with the plugin name (please see cakephp plugin model/controller cache issue with main model/controller). After that, Im facing the following problems. I have a table called users and I am using cakephp2.0.

1) I have to access the url with domain.com/admin/admin_users/index or admin/content/admin_index, instead I want to access by admin/users/index or admin/content/index. How to set this in routes in a general way so that it will be applied to all ?

2) In view, it is showing undefined index User (I have baked the views before). Everything is coming as AdminUser. After setting public $alias = 'User' this issue has been solved. Is this correct ?

3) In controller, I have to change "$this->User->some var/fn " to $this->AdminUser->some var/function is all places. Any way to solve this ?

Anything wrong with plugin name giving as admin (I have not set admin routing) ?

1

1 Answers

2
votes

You can safely name your plugin admin, this is by no means a reserved word. However, Cake doesn't take plugin names into account when parsing model / controller names, so AdminUsersController and AdminUser are actually going to be seen as classes belonging to some abstract AdminUser. So to solve all your problems using only Cake magic you'd have to actually name them User and UsersController, and hope these names don't clash. If this is not an option, you can provide some of your own magic to solve these things:

1) How to alias a controller is one of the route configuration examples in the cookbook

2) Yups, if you want to use "User" as a key, that is correct.

3) Not really using only Cake magic, because the $uses variable doesn't support aliasing. You can, however, take advantage of the new lazy-loading of 2.0, by doing something like this in your AdminUsersController:

 <?php
 public function __get($name) {
    // Note, the isset triggers lazy-loading. Check out Controller::__isset()
    // if you want to see how that works.
    if ($name == 'User' && isset($this->AdminUser)) {
        // Assign to admin user here to bypass the next time
        return ($this->User = $this->AdminUser);
    }

    // Delegate to CakePHP's magic getter
    return parent::__get($name);
}