How to Add a View Helper Directory (Zend Framework)

How to add a view helper directory (zend framework)

It can be done very easily with the built in Zend_Application resource for the view. If you're using ini configs, add a line like this:

resources.view.helperPath.My_View_Helper = "My/View/Helper"

The end of the key is the class name prefix, and the value the path where they reside.

how I add view helper in zend

According to ZF coding application structure, correct version would be:

In application.ini:

resources.view.helperPath.Your_View_Helper = "Your/View/Helper"

Then the helpers: (not sure why do you need another abstract class):

// library/Your/View/Helper/TabEntry/Abstract.php

class Your_View_Helper_TabEntry_Abstract extends Zend_View_Helper_Abstract {
public function tabEntry($param1, $param2) {} // note the lower case here
}

// library/Your/View/Helper/TabEntries.php

class Your_View_Helper_TabEntries extends Your_View_Helper_TabEntry_Abstract {
public function tabEntries($param1, $param2) {...} // note the lower case
}

In the view:

$this->tabEntries();

Important: call_user_func and Linux filesystem are case sensitive.

How to add custom view helpers to Zend Framework 2

You should add them to your module.config.php under view_helpers like this:

'view_manager' => array(
'template_path_stack' => array(
'ModuleName' => __DIR__ . '/../view',
),
),

'view_helpers' => array(
'factories' => array(
'showmessages' => function($sm) {
$helper = new ModuleName\Helper\MessageShower();
// do stuff with $sm or the $helper
return $helper;
},
),
'invokables' => array(
'selectmenu' => 'ModuleName\Helper\SelectMenu',
'prettyurl' => 'ModuleName\Helper\PrettyUrl',
),
),

Here I show two ways of creating the helpers. If all they need to do is to be instantiated, just add their name (including namespace) as invokables. If you need to do stuff with them or the ServiceManager, create them through the factories keyword.

How can I setup View helpers in the views directory?

You can have a helper named Application_View_Helper_MyHelper in application/views/helpers.

In your bootstrap you should have the following:

$view->setHelperPath(APPLICATION_PATH.'/views/helpers','Application_View_Helper');

Here, you simply register new plugin prefix for view helpers - Application_View_Helper and point Zend Framework to look for classes with this prefix in the following path - APPLICATION_PATH.'/views/helpers

Zend Framework - Access to View Helper from View

Fixed it.

The problem were the following lines in my action:

    $this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

I thought the result of my helper would be parsed into my view anyway. Bad mistake.

Assigning View Helpers ZF 1.12

Try:

$this->findurl('url');

http://framework.zend.com/manual/1.12/en/zend.view.helpers.html

Resolve view helper location from within the controller or form

Regarding Q1 (based on the comments). You should be able to access the helpers in a usual way. However since it does not work, I think there is a problem with the way you bootstrap your view resource and/or the way how you perform concrete registration of the helpers or how you add helper path to it. I paste an example of adding helper path in Bootsrap.php:

<?php
#file: APPLICATION_PATH/Bootstrapt.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

public function _initViewHelperPath() {

$this->bootstrap('view');
$view = $this->getResource('view');

$view->addHelperPath(
APPLICATION_PATH . '/modules/default/views/helpers',
'My_View_Helper' // <- this should be your helper class prefix.
);
}

}
?>

This off course should normally work for modular setup of ZF.

Regarding Q2:
You can use headScript view helper to manage what scripts do you load in the head tag of your layout. Using this helper you can do it from your actions.

For example. If in a layout.php you have:

<head>
<?php echo $this->headScript(); ?>
</head>

then in, e.g. indexAction you can append some JS file as follows:

$this->view->headScript()->appendFile($this->view->baseUrl('/js/someJS.js'));

How to register custom form view helper in Zend Framework 3

Taking an example from an active project at the company I work at. We also have some default ZF3 Form ViewHelpers overwritten with our own to interface with the front-end framework. Theme name is "Alpha" (I think ;-) )

We use the following:

'view_helpers' => [
// other stuff
'invokables' => [
'Zend\Form\View\Helper\FormCollection' => AlphaCollection::class,
'Zend\Form\View\Helper\Form' => AlphaForm::class,
'Zend\Form\View\Helper\FormRow' => AlphaRow::class,
'Zend\Form\View\Helper\FormSelect' => AlphaSelect::class,
],
],

View helper itself:

// Namespace + use statements
class AlphaCollection extends FormCollection
{
public function __construct()
{
parent::setWrapper('<div class="alpha-form-collection">%2$s%1$s%3$s</div>');
}

/**
* @param \Zend\Form\ElementInterface $element
* @param null $labelPosition
* @return string
*/
public function render(ElementInterface $element, $labelPosition = null)
{
$markup = parent::render($element, $labelPosition);

$classes = 'input-field col s12 alpha-fieldset';

if($element instanceof Collection)
{
$classes .= ' alpha-fieldset-collection';
}

$prepend = '<div class="' . $classes . '">';
$append = '</div>';

return $prepend . $markup . $append;
}
}

So in essence, we're not so much creating our own ViewHelpers so much as changing the ones provided by Zend Framework 3. Because we're just "updating" existing ones, we don't have to create new Factories (no additional requirements).

Zend Framework has registered ViewHelpers with invokable names (so you can do $this->formRow(...) or $this->formSelect(...). We've just hijacked their configuration and replaced the class we need with our own. That way, when we have a form generated completely (<?= $this->form($form) ?>), ZF does all the work for us.

Implementation in a .phtml:

<!-- Internally uses the invokables we've modified, so this is all we need to do :) -->
<?= $this->form($form) ?>

To make the configuration a bit more future proof, I think you can replace the string invokable with a FQCN nowadays (have not tested this (yet))



Related Topics



Leave a reply



Submit