How to Create Custom View Helper in ZF2

In this post I will show you how to create a custom view helper in ZF2, writing custom view helper in zf2 is very easy thank to zf2 authors.

Here in this example i am goanna create StaticFileVersioning Module, you can place this module in two different places , one is vendors directory and other option is module directory.

I prefer to keep this module in vendors directory. So my folder structure will be webroot/vendor/StaticFileVersioning. You can download at any time sample files from the below URL.

https://github.com/phpis/Static-File-Versioning—zf2-cache-buster-module

Crate a StaticVersion.php Class .

configuring it as an invokable in module.config.php

 array(
        'invokables' => array(
            'StaticVersion' => 'StaticFileVersioningViewHelperStaticVersion',
        )
    )
);

Call it in the view

$this->staticVersion("/public/css/bootstrap.css");
// output will be /public/css/bootstrap.css?v=d753e357740e330fae27bd2390734ffd

I hope this tutorial helps you please don’t forget to give us your feedback in comments.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status