how I add view helper in zend

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 02:52:36

问题


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

回答1:


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.




回答2:


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



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/3741556/how-i-add-view-helper-in-zend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!