1

I'm currently learning Zend2. My first attempt is create secured application with basic login form. So my first idea was to create a common SecuredController, that checks for user identity in his constructor and redirects if necessary. I saw that solution for Zend1 and was working:

class SecuredController extends AbstractActionController
{
    function __construct()
    {
        $auth = new AuthenticationService();

        if ( $auth->hasIdentity() ) {
            return $this->redirect()->toRoute("ts");
        }

        return $this->redirect()->toRoute( "login" );
    }
}

Then extending some other controllers used throughout appliation:

class MainController extends SecuredController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

I omitted LoginController and IndexController(same as MainController now), but you get the idea how it is set up. Confing for module looks like this:

<?php

namespace Main;

return array(
    'controllers' => array(
        'invokables' => array(
            'Main\Controller\Secured' => 'Main\Controller\Common\SecuredController',
            'Main\Controller\Login' => 'Main\Controller\LoginController',
            'Main\Controller\Main' => 'Main\Controller\MainController',
            'Main\Controller\Index' => 'Main\Controller\IndexController',
        ),
    ),

    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Main\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            ),
            'main' => array(
                'type' => 'segment',
                'options' => array(
                    'route'    => '/ts[/][:action]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Main\Controller\Main',
                        'action'     => 'index',
                    ),
                ),
            ),
            'login' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/login',
                    'defaults' => array(
                        'controller' => 'Main\Controller\Login',
                        'action' => 'login',
                    ),
                ),
            ),
            'logout' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/logout',
                    'defaults' => array(
                        'controller' => 'Main\Controller\Login',
                        'action' => 'logout',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/login' => __DIR__ . '/../view/layout/login.phtml',
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/main/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),

    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array( __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity' )
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                )
            )
        )
    ),
);

But unfortunately its not working I have error:

Url plugin requires that controller event compose a router; none found

Anyone has a clue how implement my scenario? Securing whole aplication and redirecting to /login route users without identity.

4

3 回答 3

4

添加你的超级控制器。

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Authentication\AuthenticationService;
...

class AdminController extends AbstractActionController
{

public function onDispatch(\Zend\Mvc\MvcEvent $e)
{
    /**
     * Verifica se o usuario se encontra logado, caso contrario redirecion ele para o login
     */
    $this->authService = new AuthenticationService();
    if(!$this->authService->hasIdentity()){
        $this->redirect()->toRoute("login");
    }
        return parent::onDispatch($e);
}
 ...
}

我相信这将适用于您的项目设计。

于 2013-10-26T21:10:34.463 回答
0

认证模块宽度登录页面

于 2013-04-27T06:57:11.803 回答
0

问题1)

return $this->redirect()->toRoute("ts");

您的配置中没有名为“ts”的路由,您需要在使用 toRoute() 时设置路由。

问题2)

您没有正确设置 AuthenticationService。您需要指定一个适配器才能使用它。

不要在控制器中实例化它,而是在您的 ServiceManager 配置中定义它。

配置:

'My\AuthService' => function($sm) {
    $auth = new \Zend\Authentication\AuthenticationService();
    $auth->setAdatper(/**  LDAP or What ever **/);

    return $auth;
},

控制器:

// already setup for you in the service manager
$authService = $this->getServiceLocator()->get('My\AuthService');

如果您想要开箱即用的东西允许注册用户等,您可以随时尝试 zfcUser 模块: https ://github.com/ZF-Commons/ZfcUser

于 2013-04-18T15:37:48.540 回答