【问题标题】:Zend framework 2: Define a method available in all controllersZend 框架 2:定义一个在所有控制器中都可用的方法
【发布时间】:2014-07-18 04:43:26
【问题描述】:

我怎样才能有一个方法,定义一次,在所有控制器中都可用?

在我的 UsersController 应用程序中,我有一个名为 getAuthService 的方法(它获取身份验证服务),但我也希望能够从其他控制器访问身份验证实例(这样我就可以访问它的存储)。下面是我在 UsersController 中的方法:

class UsersController {
  protected $authService

  .
  .
  .

  protected function getAuthService() {
    if (! $this->authService) {
      $dbAdapter = $this->getServiceLocator()->get('\Zend\Db\Adapter\Adapter');

      $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users', 'email', 'password', 'MD5(?)');

      $authService = new AuthenticationService();
      $authService->setAdapter($dbTableAuthAdapter);

      $this->authService = $authService;
    }

    return $this->authService;
  }
}

.. 但是,除非我在其中复制方法,否则我无法在 ApplicationController 中访问它?我可以在其他地方定义这个方法吗?或者,另一种方式?

在 Rails 中,我会将此方法放入应用程序控制器中,因为其他控制器会从那里扩展。 Zend 方法是创建包含扩展 AbstractActionController 和其他控制器扩展的共享方法的控制器,还是从 Application\Controller\IndexController 扩展其他模块的控制器:

- Zend\Mvc\Controller\AbstractActionController (abstract)
  - Application\Controller\IndexController (containing my getAuthService method)
    - Users\Controller\UsersController (extends the above so getAuthService is available)

谢谢

【问题讨论】:

    标签: zend-framework zend-framework2


    【解决方案1】:

    您可以编写一个控制器插件。由于您需要 authservice 以及 dbadapter,因此编写一个检索这些的工厂可能是个好主意。在您的application/src/Controller 中,我们添加一个名为Plugin 的文件夹。一旦完成,我们创建我们的工厂来获取所需的服务等。

    namespace Application\Controller\Plugin;
    
    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;
    
    /**
     * 
     * Your factory
     * 
     * @package Application
     */
    class AuthFactory implements FactoryInterface
    {
        /**
         * Create Service Factory
         * 
         * @param ServiceLocatorInterface $serviceLocator
         */
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $sm = $serviceLocator->getServiceLocator();
            $adapter = $sm->get('\Zend\Db\Adapter\Adapter');
    
            $plugin = new Auth();
            $plugin->setAdapter($adapter);
    
            return $plugin;
        }
    }
    

    插件可能如下所示:

    namespace Application\Controller\Plugin;
    
    use Zend\Mvc\Controller\Plugin\AbstractPlugin;
    
    class Auth extends AbstractPlugin
    {
       protected $adapter;
    
       protected $authService;
    
       public function setAdapter($adapter)
       {
          $this->adapter = $adapter;
       }
    
       public function getAdapter()
       {
          return $this->adapter;
       }
    
       public function getService()
       {
         if (! $this->authService) {
            $dbAdapter = $this->getAdapter();
    
            $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users', 'email',   'password', 'MD5(?)');
    
            $authService = new AuthenticationService();
            $authService->setAdapter($dbTableAuthAdapter);
    
            $this->authService = $authService;
          }
    
          return $this->authService;
       }
    }
    

    现在我们必须像这样将我们的工厂添加到 module.config 文件中:

    'controller_plugins' => array(
        'factories' => array(
            'auth'     => 'Application\Controller\Plugin\AuthFactory',
        ),
    ),
    

    完成后,您可以像这样在控制器中调用控制器插件:

    $this->auth()->getService();
    //or the alternative syntax
    $this->plugin('auth')->getService();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      相关资源
      最近更新 更多