2017 © Pedro Peláez
 

library zf2-asset-manager

Asset Manager for ZF2

image

shadowfax/zf2-asset-manager

Asset Manager for ZF2

  • Monday, September 16, 2013
  • by shadowfax
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

zf2-asset-manager

Yet another Zend Framework 2 asset manager., (*1)

If you are looking for a really simple and flexible asset manager for your application this could be it; but probably you are looking for another project., (*2)

Requirements

Installation

Main Setup

With composer

  1. Add this project in your composer.json:, (*3)

    "require": {
        "shadowfax/zf2-asset-manager": "dev-master"
    }
    
  2. Now tell composer to download ThemeManager by running the command:, (*4)

    $ php composer.phar update
    

By cloning project

  1. Clone this project into your ./vendor/ directory.

Post installation

  1. Enabling it in your application.config.phpfile., (*5)

    <?php
    return array(
        'modules' => array(
            'ThemeManager',
            // ...
        ),
        // ...
    );
    

Usage

This asset manager was written in order to support my ThemeManager. I just needed something simple and flexible enough so I could change the assets on the fly. In my case, if I change a theme, the AssetManager should be told to change the assets it is using., (*6)

This changes are done through the service AssetPathStack where we can add new paths for asset files, clear all paths, set new paths, etc... Lets look at an example inside a module (Module.php file):, (*7)

public function onBootstrap(MvcEvent $e)
{
    $serviceManager = $e->getApplication()->getServiceManager();
    $assetPathStack = $serviceManager->get('AssetPathStack');
    $assetPathStack->addPath( __DIR__ . '/assets');
}

With this code we are telling the AssetPathStack to add a new path to the assets folder contained in our module. Now the assets inside our module will be accessed through the assets route., (*8)

This can also be done through the module.config.php file:, (*9)

return array(
    // ...
    'asset_manager' => array(
        'paths' => array(
            __DIR__ . '/../assets'
        )
    )
);

By default the assets route is /assets, but this can be changed through the configuration files., (*10)

return array(
    // ...
    'asset_manager' => array(
        'routes' => array(
            'files' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/files',
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Wildcard'
                    )
                )
            )
        ),
    )
);

In this example we have changed the route for assets from assets to /files. We suggest this changes are made in a global configuration file such as config/autoload/assetmanager.global.php as doing this on a module.config.php could bring errors due to route collisions and the AssetManager does NOT consider modules independantly., (*11)

Multiple routes may be created. For example:, (*12)

return array(
    // ...
    'asset_manager' => array(
        'routes' => array(
            'css' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/css',
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Wildcard'
                    )
                )
            ),
            'js' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/js',
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Wildcard'
                    )
                )
            ),
            'images' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/images',
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Wildcard'
                    )
                )
            )
        ),
    )
);

This allows us to set a transparent configuration of the asset manager., (*13)

You don't have to worry about route name collisions as the AssetManager will prepend asset_manager to the route name. In the example above the route name css will be converted to asset_manager/css before it gets added to the router., (*14)

The Versions

16/09 2013

dev-master

9999999-dev https://github.com/shadowfax/zf2-asset-manager

Asset Manager for ZF2

  Sources   Download

GPLv2

The Requires

 

asset