问题
I want to add custom view helper in zend framework like this:
- I placed in application.ini this code:
includePaths.library = APPLICATION_PATH "/../library"
and create library directory in myproject root - create view helper TabEntry.php in library directory
class Zend_View_Helper_TabEntry extends Zend_View_Helper_Abstract {
public function TabEntry() {
}
} - create another view helper TabEntries.php in library directory
class Zend_View_Helper_TabEntries extends Zend_View_Helper_TabEntry {
public function TabEntries() {
}
} - when in my phtml use $this->TabEntries() get error
- 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