Reputation: 758
I have a small private Project to learn ZF2. I have integrated Zend Lucene
as a Search Function
. This works well, but I now want to integrate a Search Field in my Layout, so that it is available on all pages. I am really not sure how to achieve this. First of all, am I correct that this is done with a View Helper
? As you can see below, I have got no idea what I have to enter into the __invoke()
of my Helper to display my Search Form
. Is my way correct in general or is there a better way? I would like a good ZF2
solution, can someone give me some advice? Thank you very much in advance.
Okay, what have I done so far:
1. I created a Form:
namespace Advert\Form;
use Zend\Form\Form;
class SearchForm extends Form
{
public function __construct()
{
parent::__construct('search');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'query',
'attributes' => array(
'type' => 'text',
'id' => 'queryText',
'required' => 'required'
),
'options' => array(
'label' => 'Search String',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Search'
),
));
}
}
2. created a View Helper DisplaySearchForm.php !!! 2. UPDATE !!!
A BIG Thank you to AlexP for his help !!!
namespace Advert\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\Form\ElementInterface;
class DisplaySearchForm extends AbstractHelper
{
protected $form;
public function __construct($form)
{
$this->form = $form;
}
public function __invoke($form = null)
{
if ($form) {
$this->form = $form;
}
return $this->render($this->form);
}
public function render($form)
{
// return $this->getView()->form($form);
// To use my own Style, I have added a Partial
return $this->getView()->render('partial/search', array('form' => $form));
}
}
I read somewhere that it would not be good to use the ServiceLocator in the Helper, so I thought about doing that in a Factory, where I will then get the Form. So I created a Factory (not sure the Factory is right)
3. created Factory
namespace Advert\View\Helper;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class DisplaySearchFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator) {
$realServiceLocator = $serviceLocator->getServiceLocator();
$form = $realServiceLocator->get('FormElementManager')->get('\Advert\Form\SearchForm');
$helper = new DisplaySearchForm($form);
return $helper;
}
}
4. I registered the Factory in the module.php
public function getViewHelperConfig()
{
return array(
'factories' => array(
'displaySearchForm' => 'Advert\View\Helper\DisplaySearchForm',
)
)
}
5. In my Layout layout.phtml
<?php echo $this->displaySearchForm(); ?>
Upvotes: 0
Views: 639
Reputation: 9857
The AbstractHelper
has a getView()
which returns the 'renderer'. This means you can use all the view helpers you need, as if you were in a view script.
The new helper could look like this.
use Zend\Form\ElementInterface;
class DisplaySearchForm extends AbstractHelper
{
protected $element; // Form element to render
public function __construct(ElementInterface $element)
{
$this->element = $element;
}
public function __invoke($element = null)
{
if ($element) {
$this->element = $element;
}
return $this->render($this->element);
}
public function render(ElementInterface $element)
{
return $this->getView()->form($element);
}
}
Upvotes: 1