Zend framework Zend FW中的授权

Zend framework Zend FW中的授权,zend-framework,plugins,authentication,Zend Framework,Plugins,Authentication,我想在基于ZF的应用程序中进行授权。 在科哈纳我可以做一些像 public $auth; public $user; public function before() { parent::before(); $this->auth = Auth::instance(); $this->user = $this->auth->get_user(); // $this->user is object if user was logged

我想在基于ZF的应用程序中进行授权。 在科哈纳我可以做一些像

public $auth;
public $user;
public function before()
{
    parent::before();

    $this->auth = Auth::instance();
    $this->user = $this->auth->get_user();
    // $this->user is object if user was logged in or FALSE if not
}
在我的抽象控制器中


如何在Zend中实现同样的功能?我读过关于插件的书,认为这正是我需要的,但我没有找到任何信息来保存插件类文件以及在哪里启用它们?

您也可以在ZF中执行类似于在Kohana中所做的操作。我个人从未使用过Kohana,但我认为ZF的示例版本与此类似:

// assuming IndexController
class IndexController extends Zend_Controller_Action {

    protected $_auth;
    protected $_user;

    // you could also use init() here.
    public function preDispatch() {
        $this->_auth = Zend_Auth::getInstance();
        $this->_user = $this->_auth->getIdentity(); 
    }
}

如果您希望将其放在抽象控制器中,那么您可以创建一个扩展Zend_控制器动作的抽象控制器(例如My_控制器动作)。有了这个,IndexController将只扩展抽象控制器,而不是Zend_控制器动作。

嘿!这也很简单。但是如果你想获得授权或处理新的授权?不管怎样,两个都来了。首先使用数据库表中的凭据处理授权:

$db   = $this->getInvokeArg('bootstrap')->db;
$auth = Zend_Auth::getInstance();

$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('authLogin')
    ->setIdentityColumn('username')
    ->setCredentialColumn('password')
    ->setIdentity($username)
    ->setCredential($password);

$result = $auth->authenticate($authAdapter);

if ($result->isValid()) {
    // Yeah, logged in. Do some stuff here...
}
如果用户当前已登录,则检查如下:

$auth = Zend_Auth::getInstance();

if ($auth->hasIdentity()) {
    // User is logged in. Retrieve its identity
    $username = $auth->getIdentity();
}

希望这有帮助……

我知道我可以这样做,但是。。。我不确定这是因为Zend插件的“Zend风格”。。。无论如何,谢谢。@Chvanikoff。您可以使用插件,但有时simples解决方案是最好的。在使用ACL时,我会考虑使用插件,但是对于认证来说,这应该是足够的。实际上插件解决方案是一样的:)请使用“代码> $FruttSuxor > RealStPultin(新MyPuxLuIn Author())< /Cord>)登记它。