简体   繁体   中英

user authentication with zend framework 2

I want to add a user authentication to my project. I use the book "Webentwicklung mit Zend Framework 2" written by Michael Romer. But the first step on line 240 will not work. Here are my 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 my 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 I request the URL/login , the browser shows the message:

Login Form not yet set!

I think, there is any error between AuthController.php and AuthControllerFactory.php, but I don't now what to change.

Can someone help me?

Thanks!

Your Controller has been changed into a Factory, therefore it can no longer remain inside the invokables array. It should look like this:

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',            
    ),
    'factories' => array(
        'Application\Controller\Auth' => 'Application\Controller\AuthControllerFactory'
    )
)

Surely this is done inside the book, too.

Furthermore I suggest to not re-invent the wheel, as we have a couple of good Modules out there. ZfcUser would be what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM