2012-06-01 46 views
0

我想在我的应用程序intetegrate Zend_AclZend的__construct致命错误

class AuthController extends Zend_Controller_Action 
{ 
    public function __construct (Zend_Auth $auth, Zend_Acl $acl) 
    { 
     $this->_auth = $auth; 
     $this->_acl = $acl; 
    } 
} 

但我发现了这个错误:

Declaration of AuthController::__construct() must be compatible with that of Zend_Controller_Action_Interface::__construct() in....

任何想法?

+0

Zend_Controller_Action_Interface :: __ construct()的签名是什么样的? –

+0

看起来接口为你的构造函数保存一个声明,并且你不允许有构造函数参数。 – mazatwork

+0

所以.... :)我不知道该怎么办 –

回答

0

Zend_Controller_Action_Interface的构造是这样的:

public function __construct(Zend_Controller_Request_Abstract $request, 
          Zend_Controller_Response_Abstract $response, 
          array $invokeArgs = array()); 

这意味着你的类必须有构造

+0

我在文档中找到了这个,但我不明白。你能解释一下吗? –

+0

如果你知道'controller action'是什么,那么你就明白它处理web请求。所以很明显它需要一些参数,例如'$ request'来工作。 – WojtekT

+0

这意味着你的构造函数不能在它从Zend_Controller_Action_Interface继承的接口中没有定义任何参数。 –

0

相同的声明既然你不能改变的构造函数,你必须通过你的访问控制列表中一种不同的方式。另外请记住,您可以随时拨打Zend_Auth::getInstance()访问Zend_Auth

要访问您的ACL,您可以使用Zend_Registry。如果您要在引导程序中定义ACL,请将其放入注册表中。然后您可以通过注册表访问它,甚至可以编写一个新的类,该类扩展了Zend_Controller_Action,该类提供了一种检索ACL的方法。

0

你永远不会启动控制器实例,因为它的工作Zend_Controller_Front为你做,因此你不能使用它的构造函数。但是你可以做的是使用合适的词汇来代替

您的bootstrap.php

Zend_Registry::set('acl',$acl); 
Zend_Registry::set('auth',$auth); 

里面在您的控制器

public function init() 
{ 
    $this->_acl = Zend_Registry::get('acl'); 
    $this->_auth = Zend_Registry::get('auth'); 
} 

这个初始化函数充当构造函数。

0

,你可以使用插件:

//This class effectively extends Zend_Controller_Front 
//This plugin will dispatch with every request and provide user access control 
//based on credentials provided at login. 
class MY_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract 
{ 
    public function preDispatch(Zend_Controller_Request_Abstract $request) { 
     parent::preDispatch($request); 
     //setup ACL 
     $acl = new Zend_Acl(); 
     //add Roles 
     $acl->addRole('guest'); 
     //add Resources 
     $acl->add(new Zend_Acl_Resource('default')); 
     //set up access rules, everyone has access to the index page and the error page. 
     $acl->allow(NULL, array('index', 'error')); 
     $acl->allow(NULL, array('index', 'index')); 
     //a guest can only read content and login 
     $acl->allow('guest', 'page', array('index')); 
     //more ... 
     //fetch current user, $auth is set in loginAction() 
     $auth = Zend_Auth::getInstance(); 
     if ($auth->hasIdentity()) { 
      $identity = $auth->getIdentity(); 
      $role = strtolower($identity->role); 
     } else { 
      $role = 'guest'; 
     } 
     $module = $request->getModuleName(); 
     $controller = $request->getControllerName(); 
     $action = $request->getActionName(); 

     if (!$acl->isAllowed($role, $module, $controller, $action)) { 
      if ($role == 'guest') { 
       $request->setControllerName('user'); 
       $request->setActionName('login'); 
      } else { 
       $request->setControllerName('error'); 
       $request->setActionName('noauth'); 
      } 
     } 
    } 
} 

注册插件中的application.ini:

resources.frontController.plugins.acl = "My_Controller_Plugin_Acl" 

这只是一个(简单)的实现,有很多方法对皮肤这只猫。

希望这会有所帮助。