Download the PHP package mateusz/controllerpolicy without Composer

On this page you can find all versions of the php package mateusz/controllerpolicy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package controllerpolicy

Archived

This module has been archived because it has been superseded by functionality within Silverstripe framework.

Please set HTTP cache control headers via HTTPCacheControlMiddleware.

Controller Policy

Summary

This module is deprecated for usage with SilverStripe 3.7+. The HTTPCacheControl API in SilverStripe Framework provides a more high-level abstraction of caching behaviour.

Overview

This module has been designed to provide the ability to configure response policies that apply per specific Controller.

It comes with a small selection of policies for implementing caching:

An example Page extension PageControlledPolicy is also provided utilising CachingPolicy's ability to customise max-age based on CMS configuration on specific objects.

Installation

Install using Composer:

Note: This version supported SilverStripe 4. For a SilverStripe 3 compatible version please see the 1.x release line.

Policies

Simple policy

Let's say we want to apply a caching header of max-age 300 to the HomePage only. This module comes with a CachingPolicy which by implementing the ControllerPolicy interface can be applied to anything derived from Controller. This class can also be configured to specify the custom max-age via (injected) properties.

Using this policy is done via your project-specific config.yml. We configure the pseudo-singleton via Dependency Injection and apply it directly to HomePage_Controller:

Every policy will set headers on top of the default framework's HTTP::add_cache_headers, which is exactly what we want. This allows us to for example customise the Vary headers per policy, which were previously hardcoded. It also allows us to configure these settings on a per-controller basis, rather than a global default.

Ignoring domains

If you wish to exclude some domains from the policies completely, you can do the following:

This could be useful for example if you wish to disable caching on test servers, or if you are doing aggressive caching and want your editors to see changed resources immediately.

Vary headers

CachingPolicy also allows customisation of Vary headers through the config system:

Any URL which content depends on an impulse from the visitor should use Vary header to encode this dependency, otherwise caches might serve wrong content to the wrong user (possibly even confidential data!).

Here is a table of some more obvious Vary headers. CachingPolicy uses a relatively safe combination of Cookie, X-Forwarded-Protocol. Keep in mind the more of these you specify, the more partitioned the cache, which will nullify potential gains. Use as few as you are confident with.

Vary on Description Cache partitioning impact
Accept-Encoding Vary on content deflate method - Apache will deliver different content depending on accepted encoding. Automatically added by Apache mod_header. low
Cookie Vary on user session. Pretty much partitions the responses into generic and personalised. Note that for this to work well, the cache needs to purge frontend-only cookies such as __utma from the requests. A sensible addition. low
X-Forwarded-Protocol Vary on protocol such as http or https - use when the cache is behind a reverse-proxy, as there is often a difference in "BaseURL" which is not reflected in the URL the cache sees. A sensible addition. low
X-Forwarded-Proto A variation on X-Forwarded-Protocol, choose one appropriate to your reverse-proxy. low
Accept Vary on the response format. Some URLs, especially the API endpoints, can produce different output depending on what the user accepts: i.e. JSON vs. XML. Avoid if possible, and instead encode the content type in the URL. medium
Accept-Language Vary on the accepted language, if you are providing different language content depending on the user browser's setting. Avoid if possible, and instead encode the language in the URL. medium
User-Agent Vary on the user's device. There is so many user strings around this will effectively disable your cache. Avoid at all costs, and instead use responsive themes. extreme

Overriding policies

If you apply a policy to a certain Controller it will apply to all inheriting controllers too. For example if we have FooPageController extends PageController then the PageController policy will also affect the FooPageController.

You can break that chain easily by applying a policy to the inheriting controller as long as you are not using arrays for configuration (which you ordinarily wouldn't be - but see the "Complex policies" chapter below):

In SilverStripe 4 the config system allows you to set falsey values, which you can utilise to unset previously defined policies for a controller, or globally. This is useful for example for GET-based multi-step forms (via the silverstripe-multiform) module, where steps are traversed via GET requests, and URIs don't differ - hence preventing your from actually progressing through the form.

Note that you can also use any other policy to override the existing one - you don't only have to unset it.

PageControlledPolicy

Here is an example of how to implement CMS capability to override the max-age per specific page. In your config file put the following statements:

`

Here, applying the PageControlledPolicy extension to the Page results in a new "MaxAge" field being written into the DB, and a new tab available ("Caching") which lets the an administrative user tweak the cache max-age header (denominated in minutes).

Complex policies via array-merging

This example illustrates the usage of array-merging capability of the config system, which will enable you to simulate policy inheritance that will reflect your class diagram.

In this example we want to configure a global setting consisting of two policies, one setting the max-age to 300, and second to configure custom header. Then we want to add more specific policy for the home page max-age, while keeping the custom header. Here is how to achieve this using the config system:

Outcome of the array merging for the home page will be as follows:

We handle this array in reverse order, meaning that by default the top policy (most specific Controller) will override the others. This does not mean many Controller policies will trigger - rather, one Controller will apply a merged set.

Caution: you can either use the array syntax, or value syntax. Choose what's easier. If using an array then we recommend giving each value a key as well, which will allow you to unset previously defined values in other config blocks, which would otherwise not be possible.

Developer notes

For advanced usage of policies (e.g. returning a 304 header early) you can look at the ControllerPolicyMiddleware::process method. In such a situation, your custom policy could replace the HTTPResponse $response argument from its applyToResponse method with a new HTTPResponse with a 304 code.

Another thing is that the policies will be applied in the Controller order of initialisation, so if multiple Controllers are invoked the latter will override the former. HOWEVER this is very unlikely and has nothing to do with the inheritance of classes. This relates to how the Controller stack is invoked in SilverStripe. The extension point in ControllerPolicyApplicator has been chosen such that the ModelAsController and RootURLController do not trigger application of policies, and it is expected that only one controller will trigger the policy.


All versions of controllerpolicy with dependencies

PHP Build Version
Package Version
Requires silverstripe/framework Version ^4.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mateusz/controllerpolicy contains the following files

Loading the files please wait ....