how I add view helper in zend

2019-08-12 19:43发布

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);

4条回答
beautiful°
2楼-- · 2019-08-12 20:13

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.

查看更多
何必那么认真
3楼-- · 2019-08-12 20:34

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
查看更多
Melony?
4楼-- · 2019-08-12 20:36

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');
?>
查看更多
再贱就再见
5楼-- · 2019-08-12 20:40

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.

查看更多
登录 后发表回答