如何使用自定义帮助程序覆盖本机zf2视图帮助程序 [英] How to override the native zf2 view helpers with a custom helper

查看:101
本文介绍了如何使用自定义帮助程序覆盖本机zf2视图帮助程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的基本路径助手来替换原始的zf2基本路径视图助手.

I wanted to create a custom basepath helper to replace the original zf2 basepath view helper.

因此,如果我呼叫$this->basepath,它将使用我的自定义基本路径而不是原始的基本路径.我不确定是否可以做到.我希望我的自定义基本路径也扩展原始的基本路径类.

So if i call $this->basepath, it will use my custom basepath instead of the original one. I am not sure if this is can be done. I want my custom basepath extends the original basepath class too.

我找到了一些有关如何创建自定义助手以及如何在module.php或module.config.php中注册它们的答案

I have found some answers on how to create custom helpers and how to register them in module.php or module.config.php

但是我找不到关于如何覆盖原始助手的任何类似问题!

But i can't find any similar questions on how to override the original helpers!

推荐答案

在HelperPluginManager(

Factory definition of the basepath view helper is declared as a hardcoded invokable in HelperPluginManager (on line 45) however this definition also overridden in ViewHelperManagerFactory (line 80 to 93) because BasePath view helper requires the Request instance from ServiceLocator:

$plugins->setFactory('basepath', function () use ($serviceLocator) {
    // ...
})

我强烈建议您使用其他名称(例如, MyBasePath )扩展内置的基本路径帮助程序,而不要尝试覆盖现有的帮助程序.覆盖该本地助手可能会在以后引起一些意外的麻烦(请考虑使用该助手工作的第3方模块).

I strongly recommend extending the built-in basepath helper with a different name (MyBasePath for example) instead of trying to override the existing one. Overriding that native helper may produce some unexpected headaches later (think about 3rd party modules which uses that helper to work).

对于您的问题;是的,有可能.

For your question; yes, it is possible.

创建Application\View\Helper\BasePath.php辅助类,如下所示:

Create the Application\View\Helper\BasePath.php helper class like below:

namespace Application\View\Helper;

use Zend\View\Helper\BasePath as BaseBasePath; // This is not a typo

/**
 * Custom basepath helper
 */
class BasePath extends BaseBasePath
{
    /**
     * Returns site's base path, or file with base path prepended.
     */
    public function __invoke($file = null)
    {
        var_dump('This is custom helper');
    }
}

并在Module.php文件的onBootstrap()方法中覆盖工厂,如下所示:

And override the factory in the onBootstrap() method of the Module.php file like below:

namespace Application;

use Zend\Mvc\MvcEvent;
use Application\View\Helper\BasePath; // Your basepath helper.
use Zend\View\HelperPluginManager;

class Module
{
    /**
     * On bootstrap for application module.
     *
     * @param  MvcEvent $event
     * @return void
     */
    public function onBootstrap(MvcEvent $event)
    {
        $services = $event->getApplication()->getServiceManager();

        // The magic happens here
        $services->get('ViewHelperManager')->setFactory('basepath', function (HelperPluginManager $manager) {
            $helper = new BasePath();
            // Here you can do whatever you want with the instance before returning
            return $helper;
        });
    }
}

现在您可以在任何这样的视图中进行尝试:

Now you can try in any view like this:

echo $this->basePath('Bar');

这不是一个完美的解决方案,但应该可以.

This is not a perfect solution but it should work.

这篇关于如何使用自定义帮助程序覆盖本机zf2视图帮助程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆