PHPackages                             aklatzke/asset-funnel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. aklatzke/asset-funnel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aklatzke/asset-funnel
=====================

Auto bundler for all requested assets on a per-page basis.

063PHP

Since Jan 19Pushed 10y ago1 watchersCompare

[ Source](https://github.com/aklatzke/asset-funnel)[ Packagist](https://packagist.org/packages/aklatzke/asset-funnel)[ RSS](/packages/aklatzke-asset-funnel/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

#### aklatzke/asset-funnel

[](#aklatzkeasset-funnel)

---

A tiny and flexible asset loader with a built in cache.

\####Installation

Install [Composer](http://getcomposer.org/ "Composer").

Add the following to the `require` block in your `composer.json`:

`"aklatzke/asset-funnel" : "dev-master"`

Run `composer update` from your shell.

`require_once('/path/to/vendor/autoload.php');` within your application.

\-- ####Requirements

A way to load environment variables, and PHP &gt;= 5.6.

If using `require-dev`, [josegonzalez/phpdotenv](https://github.com/josegonzalez/php-dotenv "dotenv") will be installed as a dependency.

\-- ####Setup

First, make certain that you have the following variable defined in your environment:

`FUNNEL_STORAGE_PATH`

You can set this to any temporary or storage folder within your application. This is where the library will store the cached and concatenated files.

Import the namespace: `use Aklatzke\Funnel\Funnel`;

Next, create an instance of the funnel, with the specified output file type:

`$jsFunnel = ( new Funnel('.js') );`

`$cssFunnel = ( new Funnel('.css') );`

You may want to load the classes into a service container so that you have access to them throughout your application in order to add assets from multiple pages (to, for example, add scripts in individual partials or pages). You should refer to your individual container's documentation, just remember to create it as a singleton.

\-- ####Adding Assets

The `Funnel` class accepts both local and external resources. When a unique combination of files (both external and local) are combined using the `->bundle()` method, a unique hash is created to reference that combination of files. If this same combination is requested again, the cached and concatenated version will be used instead, resulting in a single file request rather than multiple.

Adding local resources:

`$jsFunnel->add('/absolute/path/to/file.js');`

Adding external resources:

`$jsFunnel->addExternal('http://path-to-remote/file-version-1.0.js');`

External resources are downloaded automatically, meaning that once the cache file is built, you will no longer have to send an HTTP request for that file.

You may also add arbitrary strings: `$requestLog->add('Remote IP: ' . $_SERVER['REMOTE_ADDR']);`

\-- ####Loading Resources

There are two methods to load your assets, but the first step of either is running the `bundle` method:

`$jsFunnel->bundle('/application/base/path');`

This returns a string of the file contents, so you can echo it directly into your page if you so choose:

```
