Introduction
I work through a reusable admin module; responsible for handling authentication and ACLs. This module comes with a base controller that any other implemented module can implement. Thus, this controller is Cp\AdminControllerunavailable, but inherited by all other controllers.
Problem
I have a standard / home controller Cp\HomeControllerthat has several actions; login, logout and forgotten / reset password. I'm currently working on Cp\HomeController::indexAction. As part of this method, I just do the following:
public function indexAction()
{
if ($this->getAuth()->hasIdentity()) {
} else {
$loginForm = new Form\Login();
return array(
'form' => $loginForm
);
}
}
The problem is that it Cp\HomeControlleruses a template by default ./module/Cp/view/cp/home/index.phtml; and looks like this:
<h1>Authorisation Required</h1>
<section id="admin-login">
<?= $form ?>
</section>
Zend\Form ./module/Cp/src/Cp/Form.php, . _ , , .
<?php
namespace Cp;
use Zend\Form\Form as ZendForm;
use Zend\Form\Fieldset;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
class Form extends ZendForm
{
protected $__templatePath;
protected $__viewVariables = array();
public function set($key, $value)
{
$this->__viewVariables[$key] = $value;
return $this;
}
public function setTemplatePath($path)
{
$this->__templatePath = $path;
return $this;
}
public function __toString()
{
$map = new Resolver\TemplateMapResolver(array(
'form' => $this->__templatePath
));
$renderer = new PhpRenderer();
$renderer->setResolver(new Resolver\TemplateMapResolver($map));
$view = new ViewModel();
$view->setVariable('form', $this);
$view->setTemplate('form');
foreach ($this->__viewVariables as $key => $value) {
if (! property_exists($view, $key)) {
$view->setVariable($key, $value);
}
}
return $renderer->render($view);
}
}
, , , .
<?php
namespace Cp\Form;
use Zend\Form\Element;
class Login extends \Cp\Form
{
public function __construct($name = 'Login', $action)
{
parent::__construct($name);
$this->setTemplatePath(MODULE_DIR . 'Cp/view/cp/form/login.phtml');
$email = new Element\Email('email');
$email->setLabel('E-mail Address');
$password = new Element\Password('password');
$password->setLabel('Password');
$submit = new Element\Submit('login');
$this->setAttribute('action', $action)
->setAttribute('method', 'post')
->setAttribute('autocomplete', 'autocomplete')
->add($email)
->add($password)
->add($submit);
}
}
__toString . , . (. ) , , HTML. Cp\Form\Login , , .
, Zend HTML? <input type="<?= ... ?>" name="<?= ... ?>" />. , , ; .
<section class="authentication-form">
<h2>Authentication Required</h2>
<?= $form->openTag() ?>
<? if ($ipAllowed): ?>
<p>Please choose an account to log in through.</p>
<fieldset>
<?= $form->get('email') ?>
</fieldset>
<? else: ?>
<p>Please log in using your e-mail address and password.</p>
<fieldset>
<?= $form->get('email') ?>
<?= $form->get('password') ?>
</fieldset>
<? endif ?>
<div class="action">
, , .