Skip to content
Daniel Del Rio edited this page Jan 20, 2016 · 7 revisions

This quick start aims at giving you an idea of what the asset manager is capable of. We will not be applying filters or caching. If you want to know how to apply either of these take a look at those pages in the wiki.


Let's get started

We will use a standard ZF2 skeleton application and Composer:

~$ git clone git://github.com/zendframework/ZendSkeletonApplication.git
Cloning into 'ZendSkeletonApplication'...
 
~$ cd ZendSkeletonApplication
 
~$ ./composer.phar install
Loading composer repositories with package information
Installing dependencies
  - Installing zendframework/zendframework (2.0.0)
    Downloading: 100%
 
~$ ./composer.phar require rwoverdijk/assetmanager
Please provide a version constraint for the rwoverdijk/assetmanager requirement: *
composer.json has been updated
Loading composer repositories with package information
Updating dependencies
  - Installing kriswallsmith/assetic (v1.0.4)
    Downloading: 100%
 
  - Installing rwoverdijk/assetmanager (1.0.0)
    Downloading: 100%
 
Writing lock file
Generating autoload files

Don't forget to enable the module in your config/application.config.php!

<?php
return array(
    'modules' => array(
        'AssetManager',
        'Application',
    ),
    // ... other configs ... 

Run the ZendSkeletonApplication

~$php -S 0.0.0.0:8080 -t public

Assets

To verify that everything works, we will add a simple asset that will just paint everything in our page as green (just to make things really really obvious). It will be our module/Application/public/test-asset.css (The public dir does not yet exist in the skeleton application, you will need to create it):

* {color: green;}

We now need to teach the AssetManager where to look for our assets. To do so, we will edit our module's configuration. Now let's go to module/Application/config/module.config.php, we need to add following configuration:

<?php
return array(
    'asset_manager' => array(
        'resolver_configs' => array(
            'paths' => array(
                __DIR__ . '/../public',
            ),
        ),
    ),
);

This basically means that any request that couldn't be routed to a controller will check also within our module's public/ dir and eventually serve an asset from there.

Warning! By configuring your application this way, you're basically telling PHP to serve any content from within your module's public/ dir. This also means PHP sources (as text) if there are any in there. Keep it in mind!

Checking if it works

Once you've done all this, open your browser and go to http://localhost:8080/test-asset.css. You should see the contents we wrote in the CSS file before.

That's it! You can now add your CSS file in your module/Application/view/layout/layout.phtml as following if you want:

<?php
    echo $this->headLink()->prependStylesheet($this->basePath() . '/test-asset.css');
?>

Enjoy! You can now build your modules to ship any images, js or generally assets!

Next up

Now that you have a nice working example, it's time to move on to caching and check out the other wiki pages as well. Happy coding!

Clone this wiki locally