简体   繁体   English

我如何在Zend Framework中重用代码

[英]How do I reuse code in Zend Framework

I am working on a web application which requires the user to login before they see or do anything. 我正在开发一个Web应用程序,该应用程序要求用户先登录才能看到或执行任何操作。 No part of this app should be accessible without being logged in. (Except of course, the login controller) 如果未登录,则无法访问该应用程序的任何部分。(当然,登录控制器除外)

Currently I am using sessions to handle the authentication and I have put code in each controller in the init() function to check if their session is valid. 当前,我正在使用会话来处理身份验证,并且已将代码放在init()函数的每个控制器中,以检查其会话是否有效。

This was a temporary workaround, but it is redundant and inefficient. 这是暂时的解决方法,但它是多余的且效率低下。

I would like my init() function to be similar to the following, but I am not sure how to achieve it: 我希望我的init()函数类似于以下内容,但是我不确定如何实现:

public function init()
{
    // If user not logged in redirect to login controller
    $myLibrary = Zend_Library_MyLibrary();
    $myLibrary->CheckAuth();
}

So my question really has two parts: 所以我的问题确实包含两个部分:

  1. Where is the best place to store code that will be used in multiple controllers? 存储将在多个控制器中使用的代码的最佳位置在哪里?
  2. How do I then call that function from a controller? 然后如何从控制器调用该函数?

Thanks. 谢谢。

http://zendframework.com/manual/en/zend.controller.plugins.html http://zendframework.com/manual/en/zend.controller.plugins.html

Registering a front controller plugin and hooking into an earlier part of the dispatch process is how I do it. 注册前端控制器插件并连接到调度过程的较早部分是我的方法。

$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Zend_Controller_Plugin_AuthCheck());

Put that in your index.php. 将其放在您的index.php中。

class AuthCheck extends Zend_Controller_Plugin_Abstract {
    public function preDispatch($request){
        // Check Auth

    }
}

Code that is reused across multiple controllers is best placed into an ActionHelper . 最好将跨多个控制器重用的代码放入ActionHelper中 However, for your case, I suggest to write a Controller plugin . 但是,对于您的情况,我建议编写一个Controller插件 Those hook into the Dispatch process at various stages : 这些在各个阶段都参与Dispatch流程

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
    if(!Zend_Auth::getInstance()->hasIdentity())
    {
        $request->setControllerName('auth');
        $request->setActionName('login');
        // Set the module if you need to as well.
    }
}

The above assumes you are using Zend_Auth to authenticate and manage your user identities . 以上假设您正在使用Zend_Auth验证和管理用户身份

You want a plugin over a helper, because checking if the user is logged in should happen automatically, without you having to call a checkAuth() method somewhere. 您需要一个通过助手的插件,因为检查用户是否已登录应该自动进行,而无需在某个地方调用checkAuth()方法。 Of course, nothing stops you to add an ActionHelper too, eg 当然,也没有什么可以阻止您添加ActionHelper的,例如

class My_Helper_CheckAuth extends Zend_Controller_Action_Helper_Abstract
{
    public function checkAuth()
    {
        return Zend_Auth::getInstance()->hasIdentity();
    }
    public function direct()
    {
        return $this->checkAuth();
    }
}

Once you registered your helper in the bootstrap, you could use it in every controller to check if the user is already authenticated: 在引导程序中注册了助手之后,可以在每个控制器中使用它来检查用户是否已通过身份验证:

if ( $this->_helper->checkAuth() === FALSE) {
    // do something
}

Also see these tutorials: 另请参阅以下教程:

Though, for this particular example your best bet is (probably) to use a Front Controller Plugin, you can also reuse code by extending the Zend_Controller_Action . 虽然,对于这个特定示例,最好的选择是(可能)使用前端控制器插件,但是您也可以通过扩展Zend_Controller_Action来重用代码。 Below is a contrived example, had you have been using Zend_Auth. 如果您一直在使用Zend_Auth,则下面是一个人为的示例。 This would go in library/Application/Controller and be named Action.php . 这将在library/Application/Controller并命名为Action.php If you were using a different namespace you would swap the name of the Application directory for that (library/[namespace]/Controller/Action.php) and rename the class accordingly. 如果您使用其他名称空间,则应交换该应用程序目录的名称(库/ [名称空间] /Controller/Action.php),并相应地重命名该类。

class Application_Controller_Action extends Zend_Controller_Action
{
    protected $_loggedIn;
    protected $_username;
    protected $_flashMessenger = null;

    public function init()
    {
        $auth = Zend_Auth::getInstance();
        $this->_loggedIn = $auth->hasIdentity();
        if($this->_loggedIn)
        {
            $user = $auth->getIdentity();
            $this->_username = $this->view->escape($user->username);
        }
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->initView();
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM