Cakephp插件型号名称前缀 [英] Cakephp Plugin model name prefix

查看:62
本文介绍了Cakephp插件型号名称前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Admin的插件,我的方法类似于索引,添加等。由于我想应用唯一的模型名称和控制器名称(用于ACL实现),因此我为所有控制器和模型添加了插件名称作为前缀(请参阅 cakephp插件模型/控制器缓存问题与主要模型/控制器)。之后,我面临以下问题。我有一个名为用户的表,我正在使用cakephp2.0。



1)我必须使用domain.com/admin/admin_users/index或admin /访问网址content / admin_index,相反,我想按admin / users / index或admin / content / index进行访问。



2)如何以常规方式在路由中设置它,以便将其应用于所有?



2之前的意见)。一切都以AdminUser身份出现。设置公共$ alias ='User'后,此问题已解决。这是正确的吗?



3)在控制器中,我必须将 $ this-> User-> some var / fn更改为$ this-> AdminUser-> some var / function是所有地方。有什么办法解决这个问题?



以管理员身份提供插件名称时出现任何问题(我尚未设置管理路由)?

解决方案

您可以安全地命名插件管理员,这绝不是保留字。但是,Cake在解析模型/控制器名称时不会考虑插件名称,因此 AdminUsersController AdminUser 实际上是将被视为属于某个抽象AdminUser的类。因此,要仅使用Cake magic解决所有问题,就必须将它们命名为User和UsersController,并希望这些名称不要冲突。如果这不是一个选择,则可以提供一些自己的魔术来解决这些问题:



1)如何为控制器添加别名是以下路由配置示例之一烹饪书



2)是的,如果要使用用户作为键,那是正确的。



3)不实际上只使用Cake Magic,因为 $ uses 变量不支持别名。但是,可以通过在AdminUsersController中执行类似的操作来利用2.0的新延迟加载:

 < ;?php 
公共函数__get($ name){
// //注意,isset会触发延迟加载。如果想了解它的工作原理,请查看Controller :: __ isset()
//。
if($ name =='User'&& isset($ this-> AdminUser)){
//在此处分配给admin用户以绕过下次
返回( $ this-> User = $ this-> AdminUser);
}

//委托CakePHP的神奇吸气剂
return parent :: __ get($ name);
}


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) ?

解决方案

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);
}

这篇关于Cakephp插件型号名称前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆