【问题标题】:How to use Zend Auth to make values globally available in each action & layout.phtml如何使用 Zend Auth 使每个动作和 layout.phtml 中的值全局可用
【发布时间】:2013-09-26 09:58:35
【问题描述】:

我通过在Module.php 中创建getServiceConfig() 类似AuthenticationService 的对象来实现Zend Auth:

'AuthService' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'tblUSRUsers', 'Email', 'Password', "MD5(?)");

                    $authService = new AuthenticationService();
                    $authService->setAdapter($dbTableAuthAdapter);
                    $authService->setStorage($sm->get('Application\Model\MyAuthStorage'));

                    return $authService;
                },

在控制器动作中,我通过

获取这个对象
$this->getServiceLocator()
                    ->get('AuthService');

对于身份验证,我使用

获取值
$authservice    = $this->getServiceLocator()->get('AuthService');
$arrUserDetails = $authservice->getIdentity();

它工作正常,这些值可用。

但问题是 ServiceLocator 在 Controller 构造函数中不可用,因此无法在那里编写上述代码。在每个动作中编写此代码似乎不是一个好习惯。 有人可以帮忙吗?

【问题讨论】:

    标签: php zend-framework2 zend-auth service-locator


    【解决方案1】:

    可能的解决方案是拥有一个路由事件处理程序,您可以在其中设置用户凭据:

    class Module
    {
        public function onBootstrap(MvcEvent $e)
        {
            $eventManager = $e->getApplication()
                ->getEventManager ();
    
            $eventManager->attach (
                MvcEvent::EVENT_ROUTE,
                function  (MvcEvent $e)
                {
                    $auth = $e->getApplication()
                              ->getServiceManager()
                              ->get('AuthService');
    
                    $e->setParam('userinfo', $auth->getUserInfo());
                    // update 
                    $layout = $e->getViewModel();
                    $layout->userinfo = $auth->getUserInfo();           
           });
        }
    

    然后像这样在控制器中访问 then:

    class IndexController extends AbstractActionController
    {
        public function indexAction ()
        {
            $this->getEvent()->getParam('userinfo');
    

    【讨论】:

    • 假设我必须将登录的用户名传递给 layout.phtml。我想通过布局的构造函数设置这些信息。但是,$this->getEvent()->getParam('userinfo');在构造函数中也不可用。所以,强迫我把它放在每个动作中。
    • 谢谢。如果未经身份验证,我还想将用户重定向到登录页面。为此,我正在写 if(!$auth->hasIdentity()) { $controller = $e->getTarget(); return $controller->plugin('redirect')->toRoute('login'); } 在 onBootstrap 函数本身。但是,它显示“Fatal error: Call to undefined method Zend\Mvc\Application::redirect()” 您能否请说明如何实现?
    【解决方案2】:

    有几种方法可以做到这一点。您可以在 Module 类中重载 ControllerConfig 方法,以便您可以根据需要实例化控制器。或者您可以编写一个控制器插件来处理身份验证并返回身份。

    编辑

    应该早点提供这些链接,但没有太多的手机访问权限;)。

    要了解 ControllerConfig,请查看 this blog post。 Module 类中 getControllerConfig 方法的一半有一个解释。这是模块管理器在实例化模块类时寻找的一种方法,它允许您自定义该模块中控制器的实例化。一旦您返回 Controller 类,Controller Manager 就会注入其余的 Controller 依赖项(插件等)

    Controller plugins 是注入到每个控制器的小类,并且可以从操作中调用以执行一些常见的代码。当您在控制器操作中使用 $this->getServiceLocator() 时,您使用的是插件。

    【讨论】:

    • 我不熟悉这些。能详细解释一下吗?
    • 查看我的编辑。您可能还想查看github.com/ZF-Commons/ZfcUser 以及他们如何构建自己的 Module 类。
    猜你喜欢
    • 2014-09-07
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多