\Dewdrop\View\HelperUrlCachePrefix

This helper can be used to prepend a string to URLs to assist with busting HTTP caches when pushing a new build or working in development. Used in conjunction with a rewrite rule that strips the cache prefix before looking for the file on disk, it allows you to avoid issues caused by stale caches without having to rename the actual files on disk.

In Apache, for example, you could add the following mod_rewrite rule:

RewriteEngine On

# Strip cache-busting prefixes from URLs
RewriteRule ^cache-[\dA-Z.\-_]+/(.+) $1 [L,NC]

And then set the prefix of this helper to cache-build-1234. When you adjust your prefix to cache-build-1235, all the URLs filtered by this helper will change, causing HTTP caches to be invalidated. With this URL invalidation setup in place, you can also use far-future expires headers for your static content, which will improve performance significantly. In Apache, you can set those headers with this addition to your .htaccess:

ExpiresActive On
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/gif "access plus 10 years"
ExpiresByType image/jpeg "access plus 10 years"
ExpiresByType image/png "access plus 10 years"
ExpiresByType text/css "access plus 10 years"
ExpiresByType text/javascript "access plus 10 years"
ExpiresByType application/javascript "access plus 10 years"
ExpiresByType application/x-javascript "access plus 10 years"

Summary

Methods
Properties
Constants
__construct()
partial()
direct()
url()
setPrefix()
getPrefix()
headLink()
headScript()
No public properties found
No constants found
delegateIfArgsProvided()
delegateByArgs()
checkRequired()
getHelperName()
ensurePresent()
ensureArray()
$view
N/A
No private methods found
$prefix
N/A

Properties

$view

$view : \Dewdrop\View\View

The view that instantiated this helper.

Type

\Dewdrop\View\View

$prefix

$prefix : string

The prefix to prepend to all URLs filtered by this helper.

Type

string

Methods

__construct()

__construct(\Dewdrop\View\View  $view) 

Create helper, accepting a reference to the related view object

Parameters

\Dewdrop\View\View $view

partial()

partial(string  $name, array  $data) : string

Render a partial view script.

Generally, your helper should render HTML with partial view scripts rather than generating the markup in the helper class directly. This makes it easier for frontend developers to make modifications to the HTML.

The $data parameter should contain key-value pairs for each variable you'd like available in your partial view.

Parameters

string $name
array $data

Returns

string —

The rendered output

direct()

direct() : $this|string

The helper instance or, if a string is supplied as the first parameter, the filtered URL.

Returns

$this|string

url()

url(  $url) : string

Prepend the prefix to the supplied URL if it doesn't start with a protocol designation or "//" (the http/https agnostic URL prefix).

Parameters

$url

Returns

string

setPrefix()

setPrefix(string  $prefix) : $this

Set the prefix you'd like to prepend to filtered URLs. Typically, you'll set this in your Pimple definition of your view object in your application's bootstrap so that it is available consistently throughout your app.

Parameters

string $prefix

Returns

$this

getPrefix()

getPrefix() : string

Get the prefix that should be used for cached URLs.

Returns

string

headLink()

headLink(\Zend\View\Helper\HeadLink  $headLink) : \Zend\View\Helper\HeadLink

Modify all URLs in the supplied HeadLink view helper to include the URL prefix and then return it.

Parameters

\Zend\View\Helper\HeadLink $headLink

Returns

\Zend\View\Helper\HeadLink

headScript()

headScript(\Zend\View\Helper\HeadScript  $headScript) : \Zend\View\Helper\HeadScript

Modify all URLs in the supplied HeadLink view helper to include the URL prefix and then return it.

Parameters

\Zend\View\Helper\HeadScript $headScript

Returns

\Zend\View\Helper\HeadScript

delegateIfArgsProvided()

delegateIfArgsProvided(array  $args, string  $method = 'directWithArgs') : $this|string

Useful in cases where you want to just return the helper if the user didn't provide any args to direct(), but want to call a short-cut method if they did.

Parameters

array $args
string $method

Returns

$this|string

delegateByArgs()

delegateByArgs(array  $args, string  $methodPrefix) : mixed

Delegate to one of three methods depending upon the contents of the $args array:

  • If $args[0] is an instance of \Dewdrop\Db\Field, call "{$methodPrefix}Field".
  • If $args[0] is an array, call "{$methodPrefix}Array".
  • Otherwise, call "{$methodPrefix}Explicit".

Parameters

array $args

The arguments to pass the delegated method.

string $methodPrefix

Returns

mixed

checkRequired()

checkRequired(array  $options, array  $required) : \Dewdrop\View\Helper\AbstractHelper

Check that the values in $required are present as keys in $options.

Use this in methods accepting parameters as an array of key-value pairs to ensure that required parameters are present. If one of the required parameters is absent, an exception is thrown.

Parameters

array $options
array $required

Throws

\Dewdrop\Exception

Returns

\Dewdrop\View\Helper\AbstractHelper

getHelperName()

getHelperName() : string

Get the helper name, which is the suffix at the end of the fully qualified class name after the final namespace separator ("\").

Returns

string

ensurePresent()

ensurePresent(array  $options, array  $present) : \Dewdrop\View\Helper\AbstractHelper

Ensure that the values in $present are keys in $options.

If the key is absent from $options, it will be added with a null value. Therefore, this method differs from checkRequired() in that the user must supply a value (even if that value is null) before checkRequired(), but for ensurePresent() the key will simply be added if the user hadn't already specified a value.

Note: Notice that the $options parameter is handled by-reference to allow creation of the missing keys while still allowing chaining to other methods.

Parameters

array $options
array $present

Returns

\Dewdrop\View\Helper\AbstractHelper

ensureArray()

ensureArray(array  $options, array  $isArray) : \Dewdrop\View\Helper\AbstractHelper

Ensure that the values in the $isArray parameter are present in $options as an array.

If the option's current value is null, it will be converted to an array. If the option's current value is not null but also not an array, it will be wrapped in an array. For example, if you had a "classes" option that let the user specify one or more CSS classes, they could use a string to define a single class and this method would wrap that single value in array array to make the handling of the various options simpler and more consistent for the view helper developer.

Note: Notice that the $options parameter is handled by-reference to allow creation of the missing keys while still allowing chaining to other methods.

Parameters

array $options
array $isArray

Returns

\Dewdrop\View\Helper\AbstractHelper