# How To Use Plugins

Before you can use a plugin, you must install and enable it first.
See [How To Install Plugins](https://pieceofcake2.github.io/docs/plugins/how-to-install-plugins.md).

## Plugin Configuration

There is a lot you can do with the load and loadAll methods to help with
plugin configuration and routing. Perhaps you want to load all plugins
automatically, while specifying custom routes and bootstrap files for
certain plugins.

No problem. In `config/bootstrap.php`:

```php
CakePlugin::loadAll([
    'Blog' => ['routes' => true],
    'ContactManager' => ['bootstrap' => true],
    'WebmasterTools' => ['bootstrap' => true, 'routes' => true],
]);
```

With this style of configuration, you no longer need to manually
include() or require() a plugin's configuration or routes file--It happens
automatically at the right time and place. CakePHP loads the plugin's
`Config/bootstrap.php` and `Config/routes.php` files, for example
`plugins/Blog/Config/routes.php`. The exact same parameters could
have also been supplied to the load() method, which would have loaded only those
three plugins, and not the rest.

A plugin bootstrap can also register DSN schemes for datasources and email
transports (see
[Registering DSN Schemes](https://pieceofcake2.github.io/docs/plugins/how-to-create-plugins.md#registering-dsn-schemes)).
Load such a plugin with `'bootstrap' => true`.

Finally, you can also specify a set of defaults for loadAll which will apply to
every plugin that doesn't have a more specific configuration.

Load the bootstrap file from all plugins, and additionally the routes from the Blog plugin:

```php
CakePlugin::loadAll([
    ['bootstrap' => true],
    'Blog' => ['routes' => true],
]);
```

Note that all files specified should actually exist in the configured
plugin(s) or PHP will give warnings for each file it cannot load. This is
especially important to remember when specifying defaults for all plugins.

CakePHP 2.3.0 added an `ignoreMissing` option, that allows you to ignore any
missing routes and bootstrap files when loading plugins. You can shorten the
code needed to load all plugins using this:

```php
// Loads all plugins including any possible routes and bootstrap files
CakePlugin::loadAll([
    ['routes' => true, 'bootstrap' => true, 'ignoreMissing' => true],
]);
```

Some plugins additionally need to create one or more tables in your database. In
those cases, they will often include a schema file which you can
call from the cake shell like this:

```bash
bin/cake schema create --plugin ContactManager
```

Most plugins will indicate the proper procedure for configuring
them and setting up the database in their documentation. Some
plugins will require more setup than others.

## Advanced Bootstrapping

If you like to load more than one bootstrap file for a plugin. You can specify
an array of files for the bootstrap configuration key:

```php
CakePlugin::loadAll([
    'Blog' => [
        'bootstrap' => [
            'config1',
            'config2',
        ],
    ],
]);
```

You can also specify a callable function that needs to be called when the plugin
has been loaded:

```php
function aCallableFunction($pluginName, $config)
{
}

CakePlugin::loadAll([
    'Blog' => [
        'bootstrap' => 'aCallableFunction',
    ],
]);
```

## Using a Plugin

You can reference a plugin's controllers, models, components,
behaviors, and helpers by prefixing the name of the plugin before
the class name.

For example, say you wanted to use the ContactManager plugin's
ContactInfoHelper to output some pretty contact information in
one of your views. In your controller, your \$helpers array
could look like this:

```php
public $helpers = ['ContactManager.ContactInfo'];
```

You would then be able to access the ContactInfoHelper just like
any other helper in your view, such as:

```php
echo $this->ContactInfo->address($contact);
```
