php - zend framework2 proper way to basic login authentication -
i'm learning zend2. first attempt create secured application basic login form. first idea create common securedcontroller, checks user identity in constructor , redirects if necessary. saw solution zend1 , 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 other controllers used throughout appliation:
class maincontroller extends securedcontroller { public function indexaction() { return new viewmodel(); } }
i omitted logincontroller , indexcontroller(same maincontroller now), idea how set up. confing module looks 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 not working have error:
url plugin requires controller event compose router; none found
anyone has clue how implement scenario? securing whole aplication , redirecting /login route users without identity.
add in super controller.
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); } ... }
i believe work in project design.
Comments
Post a Comment