Plugins are the primary way to alter or intercept requests and responses in Alloy. Plugins are also the primary way to add new functionaliy to Alloy by extending the Kernel with your own dynamic methods, add filters, and observe triggered events.

Creating Plugins

Plugins can either be standalone plugins placed in the app/Plugin directory, or a Module plugin inside the app/Module directory. The most basic plugin consists of a single file named Plugin.php inside a named directory that the plugin will be referenced by, like 'Oauth'. Plugins, like Modules, are designed to be fully self-contained within a single folder for maximum portability between projects and for easier disribution and packaging. Also like Modules, Plugins can be as complex as you want them to be, and can contain and activate other plugins, libraries, and class files as sub-packages, as many levels deep as you need.

This example plugin code below adds the Symfony2 Finder Component to your Alloy project.

File: app/Plugin/Finder/Plugin.php

namespace Plugin\Finder; use Alloy; /** * Finder Plugin * Adds the finder and file iterator from Symfony2 */ class Plugin { public function __construct(Alloy\Kernel $kernel) { // Let autoloader know where to find Symfony Finder library files $kernel->loader()->registerNamespace('Symfony\Component\Finder', __DIR__ . '/lib'); // Make methods globally avaialble with Kernel $kernel->addMethod('finder', function() { return new \Symfony\Component\Finder\Finder(); }); } }


The Finder plugin code above does two things:

  1. Registers the included lib directory as an autoload location for the Symfony\Component\Finder namespace
  2. Adds a new dynamic finder method on the Kernel that allows other code in your Alloy project to use the Finder component with $kernel->finder()

The Finder plugin has a folder hierarchy that looks like this:

  • alloy/
  • app/
    • Plugin/
      • Finder/
        • Plugin.php
        • lib/
          • Symfony/
            • Component/
              • Finder/
                • Finder.php
                • Glob.php
                • ... etc ...

Notice how the Symfony Finder Component library files are inside the app/Plugin/Finder/ directory itself. The library could technically be placed anywhere as long as the autolaoder knows where to find it, but this organization allows the Plugin to be moved from project to project by copying a single plugin folder.

Enabling and Disabling Plugins

Plugins must be explicitly enabled by adding them to the plugins configuration setting.

// Plugins loaded $cfg['app']['plugins'] = array( 'Alloy_Layout', # alloy/Plugin/Alloy/Layout 'Spot', # alloy/Plugin/Spot 'Finder' # app/Plugin/Finder );


Related Links