php - user authentication with zend framework 2 -
i want add user authentication project. use book "webentwicklung mit zend framework 2" written michael romer. first step on line 240 not work. here files:
authcontroller.php:
<?php namespace application\controller; use application\form\loginform; use zend\mvc\controller\abstractactioncontroller; use zend\view\model\viewmodel; class authcontroller extends abstractactioncontroller { private $loginform; public function loginaction() { if(!$this->loginform) { throw new \badmethodcallexception('login form not yet set!'); } //var_dump($this); if($this->getrequest()->ispost()) { $this->loginform->setdata($this->getrequest()->getpost()); if($this->loginform->isvalid()) { var_dump($this->loginform->getdata()); exit; } else { return new viewmodel( array( 'form' => $this->loginform ) ); } } else { return new viewmodel( array( 'form' => $this->loginform ) ); } } public function setloginform($loginform) { $this->loginform = $loginform; } public function getloginform() { return $this->loginform; } }
loginform.php:
<?php namespace application\form; use zend\form\form; class loginform extends form { public function __construct() { parent::__construct('loginform'); $this->setattribute('action', '/login'); $this->setattribute('method', 'post'); $this->setinputfilter(new \application\form\loginfilter()); $this->add(array( 'name' => 'username', 'attributes' => array( 'type' => 'text', ), 'options' => array( 'label' => 'benutzername:', ) )); $this->add(array( 'name' => 'password', 'attributes' => array( 'type' => 'password', ), 'options' => array( 'label' => 'password:', ) )); $this->add(array( 'name' => 'submit', 'attributes' => array( 'type' => 'submit', 'value' => 'einloggen' ), )); } }
loginfilter.php:
<?php namespace application\form; use zend\form\form; use zend\inputfilter\inputfilter; class loginfilter extends inputfilter { public function __construct() { $this->add(array( 'name' => 'username', 'required' => true, )); $this->add(array( 'name' => 'password', 'required' => true, )); } }
login.phtml:
<?php $this->form->prepare(); echo $this->form()->opentag($this->form); echo $this->formrow($this->form->get('username')); echo $this->formrow($this->form->get('password')); echo $this->formsubmit($this->form->get('submit')); echo $this->form()->closetag();
authcontrollerfactory.php:
<?php namespace application\controller; use zend\servicemanager\factoryinterface; use zend\servicemanager\servicelocatorinterface; class authcontrollerfactory implements factoryinterface { public function createservice(servicelocatorinterface $servicelocator) { $ctr = new authcontroller(); print_r("hier"); die(); $ctr->setloginform(new \application\form\loginform()); return $ctr; } }
and parts of module.config.php:
... return array( 'router' => array( 'routes' => array( 'login' => array( 'type' => 'literal', 'options' => array( 'route' => '/login', 'defaults' => array( '__namespace__' => 'application\controller', 'controller' => 'auth', 'action' => 'login', ), ), ), ... 'controllers' => array( 'invokables' => array( 'application\controller\index' => 'application\controller\indexcontroller', 'application\controller\auth' => 'application\controller\authcontroller' ), ...
when request url/login , browser shows message:
login form not yet set!
i think, there error between authcontroller.php , authcontrollerfactory.php, don't change.
can me?
thanks!
your controller has been changed factory, therefore can no longer remain inside invokables
array. should this:
'controllers' => array( 'invokables' => array( 'application\controller\index' => 'application\controller\indexcontroller', ), 'factories' => array( 'application\controller\auth' => 'application\controller\authcontrollerfactory' ) )
surely done inside book, too.
furthermore suggest not re-invent wheel, have couple of modules out there. zfcuser
you're looking for.
Comments
Post a Comment