我是 Zend 框架的新手,目前正在研究 Zend_Acl 。网上有很多例子。在很多这样的例子中,你会看到这样的代码:
class My_Acl extends Zend_Acl {
public function __construct() {
//Add a new role called "guest"
$this->addRole(new Zend_Acl_Role('guest'));
//Add a role called user, which inherits from guest
$this->addRole(new Zend_Acl_Role('user'), 'guest');
//Add a resource called page
$this->add(new Zend_Acl_Resource('page'));
//Add a resource called news, which inherits page
$this->add(new Zend_Acl_Resource('news'), 'page');
//Finally, we want to allow guests to view pages
$this->allow('guest', 'page', 'view');
//and users can comment news
$this->allow('user', 'news', 'comment');
}
}
所以基本上 - 我们扩展了 Zend_Acl 类,我们在其中定义了角色和资源。我有点不明白为什么我们要创建单独的类而不是在引导程序中的例如资源方法中做同样的事情,然后将结果推送到注册表中?
像这样的前:
protected function _initAcl()
{
$myacl = new Zend_Acl();
$myacl->addRole(new Zend_Acl_Role('guest'));
$myacl->addRole(new Zend_Acl_Role('user'), 'guest');
$myacl->add(new Zend_Acl_Resource('page'));
$myacl->add(new Zend_Acl_Resource('news'), 'page');
$myacl->allow('guest', 'page', 'view');
$myacl->allow('user', 'news', 'comment');
Zend_Registry::set('acl', $myacl);
}
我认为这些方法会给我们带来相同的结果是否正确?
谢谢!ps 该死的zend很复杂