0

ZF2 で Layout.phtml のルートにリダイレクトすることは可能ですか。ユーザーがlayout.phtmlからログインしていない場合、ログインページにリダイレクトしたい

これまでのところ、私は試しました:

<?php $auth = new AuthenticationService();
                      if ($auth->hasIdentity()) {?>
                        <li class="active"><a href="<?php echo $this->url('home') ?>">
                        <?php echo $this->translate('Home') ?></a></li>
                        <li class="active"><a href="<?php echo $this->url('login/process', array('action'=>'logout')) ?>"><?php echo $this->translate('Logout') ?></a></li>
                  <?php 
                     }  
                   else  
                    {
                   $this->_forward('login/process');

                     } ?>

「_forward のインスタンスを取得または作成できませんでした」というエラーが表示されます

ブートストラップ コード:

  public function onBootstrap(MvcEvent $e)
{
    $e->getApplication()->getServiceManager()->get('translator');
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $eventManager = $e->getApplication()->getEventManager();
    //nothing's available for non logged user, so redirect him to login page
    $eventManager->attach("dispatch", function($e) {
        $match = $e->getRouteMatch();
        $list = $this->whitelist;
                    // Route is whitelisted
                    $name = $match->getMatchedRouteName();
                    if (in_array($name, $list)) {
                        return;
                    }
        $sm = $e->getApplication()->getServiceManager();
        $controller = $e->getTarget();
        $auth = $sm->get('AuthService');
        if (!$auth->hasIdentity() && $e->getRouteMatch()->getMatchedRouteName() !== 'login/process') {
            $application = $e->getTarget();

            $e->stopPropagation();
            $response = $e->getResponse();
            $response->setStatusCode(302);
            $response->getHeaders()->addHeaderLine('Location', $e->getRouter()->assemble(array(), array('name' => 'login/process')));
            //returning response will cause zf2 to stop further dispatch loop

            return $response;
        }
    }, 100);
}
4

1 に答える 1

1

これは、 の中でやりたいことではありませんlayout.phtml。通常、レンダリングの前に発生するイベントにフックします。ZF2 では、そのようなものにフックする最も早いイベントは、ルート イベントになります。Authorization-Module BjyAuthorizeで使用されるプロセスの優れた図は、それを非常によく説明しています。

承認ワークフロー BjyAuthorize

モジュールを使用したくない場合は、次のように、そこで何が起こっているかを縮小することもできます。

//class Module
public function onBootstrap(MvcEvent $mvcEvent) 
{
    $eventManager = $mvcEvent->getApplication()->getEventManager();
    $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'), -1000);
}

public function onRoute(MvcEvent $event) 
{
    $serviceLocator = $mvcEvent->getApplication()->getServiceLocator();
    // From this point onwards you have access to the ServiceLocator and can check
    // for an authenticated user and if the user is not logged in, you return a 
    // Response object with the appropriate ResponseCode redirected and that's it :)
}
于 2013-08-20T16:07:28.677 に答える