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.