how I add view helper in zend
Asked Answered
Z

4

1

I want to add custom view helper in zend framework like this:

  1. I placed in application.ini this code:

    includePaths.library = APPLICATION_PATH "/../library"
    and create library directory in myproject root

  2. create view helper TabEntry.php in library directory

    class Zend_View_Helper_TabEntry extends Zend_View_Helper_Abstract {

    public function TabEntry() {

    }
    }

  3. create another view helper TabEntries.php in library directory

    class Zend_View_Helper_TabEntries extends Zend_View_Helper_TabEntry {

    public function TabEntries() {

    }
    }

  4. when in my phtml use $this->TabEntries() get error
  5. in Bootstrap.php I add some code:
    $view->addHelperPath('MyView/Helpers', "library_MyView_Helpers");
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); $viewRenderer->setView($view);
Zeuxis answered 18/9, 2010 at 11:42 Comment(3)
chouldn'T it be $this->tabEntries() ? :)Cassiopeia
And what kind of error you get?Cassiopeia
this error Class 'Zend_View_Helper_TabEntry' not foundStratovision
T
2

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.

Topper answered 18/9, 2010 at 11:42 Comment(3)
By doing this I get this error: Fatal error: Class 'MyView_Helpers_TabEntry' not found in D:\Program Files\Zend\Apache2\htdocs\tab_cms\library\MyView\Helpers\TabEntries.php on line 3Stratovision
@Zeuxis Double check your file names and ensure you are following PEAR naming conventions.Topper
dear takeshin file names are correct, I'm really confused I want to use only one class in another OOP !!!Stratovision
B
0

Add helper in zend 3

create helper class Helper.php in module/Admin/src/View/Helper/Helper.php

after that add the following code in:

<?php

namespace Admin\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Helper extends AbstractHelper {
    public function test($messages) {
        echo $messages;
    }
}

module/Admin(your module name)/config/module.config.php

use Zend\ServiceManager\Factory\InvokableFactory;

'view_helpers' =>[
    'factories' => [
        View\Helper\Helper::class => InvokableFactory::class,
    ],
    'aliases' => [
        'mainHelper' => View\Helper\Helper::class
    ],
],

call on view

<?php
$this->mainHelper()->test('Abhishek');
?>
Berner answered 18/9, 2010 at 11:42 Comment(0)
P
0

Zend framework/located in /var/www/html/you

1) you/application/views/helpers/<Magic is here>

2) Put this file called "Stuff.php" in above path

<?php
class Zend_View_Helper_Stuff extends Zend_View_Helper_Abstract 
{
  public function stuff()
  {
    $output = "IK BEN View Helper en DAN????";
    return htmlspecialchars($output);    
  }
}

?>

3) Go to you/application/views/scripts/index/index.phtml <?= $this->stuff(); ?> :)

4) output will be the $output.

Prank answered 18/9, 2010 at 11:42 Comment(0)
L
0

Double check the code you have in your bootstrap

in Bootstrap.php I add some code:

$view->addHelperPath('MyView/Helpers', "library_MyView_Helpers");
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);

Should be more like

$view->addHelperPath('My/View/Helpers', "My_View_Helpers");
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);

On my side, I use:

// Add path to project view helpers
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->initView();
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view
    ->addHelperPath('Julien/View/Helper', 'Julien_View_Helper')
    ;

and have that kind of class in Julien/View/Helper/Percent.php

<?php
class Julien_View_Helper_Percent extends Zend_View_Helper_Abstract {

    public function percent ( $percentage ) {
        return $percentage * 100 . '%';
    }
}

then calling in the view

<?= $this->percent(0.255) ?>

will output

25.5%

and my directory layout looks like

project/
    lib/
        Julien/
            View/
                Helper/
                     Percent.php
        Zend/
    modules/
public/
   .htaccess 
   index.php
Loehr answered 18/9, 2010 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.