Class CakePlugin

CakePlugin is responsible for loading and unloading plugins.

It also can retrieve plugin paths and load their bootstrap and routes files.

Package: Cake\Core
Link: https://book.cakephp.org/2.0/en/plugins.html
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Core/CakePlugin.php

Properties summary

  • $_plugins protected static
    array
    Holds a list of all loaded plugins and their configuration

Method Summary

  • _includeFile() protected static
    Include file, ignoring include error if needed if file is missing
  • bootstrap() public static
    Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  • load() public static
    Loads a plugin and optionally loads bootstrapping, routing files or loads an initialization function
  • loadAll() public static
    Will load all the plugins located in the configured plugins folders
  • loaded() public static

    Returns true if the plugin $plugin is already loaded If plugin is null, it will return a list of all loaded plugins

  • path() public static
    Returns the filesystem path for a plugin
  • routes() public static
    Loads the routes file for a plugin, or all plugins configured to load their respective routes file
  • unload() public static
    Forgets a loaded plugin or all of them if first parameter is null

Method Detail

_includeFile()source protected static

_includeFile( string $file , boolean $ignoreMissing false )

Include file, ignoring include error if needed if file is missing

Parameters

string $file
File to include
boolean $ignoreMissing optional false
Whether to ignore include error for missing files

Returns

mixed

bootstrap()source public static

bootstrap( string $plugin )

Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration

Parameters

string $plugin
name of the plugin

Returns

mixed

See

CakePlugin::load() for examples of bootstrap configuration

load()source public static

load( string|array $plugin , array $config array() )

Loads a plugin and optionally loads bootstrapping, routing files or loads an initialization function

Examples:

CakePlugin::load('DebugKit');

Will load the DebugKit plugin and will not load any bootstrap nor route files.

CakePlugin::load('DebugKit', array('bootstrap' => true, 'routes' => true));

Will load the bootstrap.php and routes.php files.

CakePlugin::load('DebugKit', array('bootstrap' => false, 'routes' => true));

Will load routes.php file but not bootstrap.php.

CakePlugin::load('DebugKit', array('bootstrap' => array('config1', 'config2')));

Will load config1.php and config2.php files.

CakePlugin::load('DebugKit', array('bootstrap' => 'aCallableMethod'));

Will run the aCallableMethod function to initialize it.

Bootstrap initialization functions can be expressed as a PHP callback type, including closures. Callbacks will receive two parameters (plugin name, plugin configuration).

It is also possible to load multiple plugins at once. Examples:

CakePlugin::load(array('DebugKit', 'ApiGenerator'));

Will load the DebugKit and ApiGenerator plugins.

CakePlugin::load(array('DebugKit', 'ApiGenerator'), array('bootstrap' => true));

Will load bootstrap file for both plugins.

CakePlugin::load(array(
            'DebugKit' => array('routes' => true),
            'ApiGenerator'
        ),
     array('bootstrap' => true)
 );

Will only load the bootstrap for ApiGenerator and only the routes for DebugKit. By using the path option you can specify an absolute path to the plugin. Make sure that the path is slash terminated or your plugin will not be located properly.

Parameters

string|array $plugin
name of the plugin to be loaded in CamelCase format or array or plugins to load
array $config optional array()
configuration options for the plugin

Throws

MissingPluginException
if the folder for the plugin to be loaded is not found

loadAll()source public static

loadAll( array $options array() )

Will load all the plugins located in the configured plugins folders

If passed an options array, it will be used as a common default for all plugins to be loaded It is possible to set specific defaults for each plugins in the options array. Examples:

CakePlugin::loadAll(array(
        array('bootstrap' => true),
        'DebugKit' => array('routes' => true, 'bootstrap' => false),
    ));

The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file and will not look for any bootstrap script. If you are loading many plugins that inconsistently support routes/bootstrap files, instead of detailing each plugin you can use the ignoreMissing option:

CakePlugin::loadAll(array(
    'ignoreMissing' => true,
    'bootstrap' => true,
    'routes' => true,
 ));

The ignoreMissing option will do additional file_exists() calls but is simpler to use.

Parameters

array $options optional array()
Options list. See CakePlugin::load() for valid options.

loaded()source public static

loaded( string $plugin null )

Returns true if the plugin $plugin is already loaded If plugin is null, it will return a list of all loaded plugins

Parameters

string $plugin optional null
Plugin name to check.

Returns

mixed

boolean true if $plugin is already loaded. If $plugin is null, returns a list of plugins that have been loaded


path()source public static

path( string $plugin )

Returns the filesystem path for a plugin

Parameters

string $plugin
name of the plugin in CamelCase format

Returns

string
path to the plugin folder

Throws

MissingPluginException
if the folder for plugin was not found or plugin has not been loaded

routes()source public static

routes( string $plugin null )

Loads the routes file for a plugin, or all plugins configured to load their respective routes file

Parameters

string $plugin optional null

name of the plugin, if null will operate on all plugins having enabled the loading of routes files

Returns

boolean

unload()source public static

unload( string $plugin null )

Forgets a loaded plugin or all of them if first parameter is null

Parameters

string $plugin optional null
name of the plugin to forget

Properties detail

$_pluginssource

protected static array

Holds a list of all loaded plugins and their configuration

array()

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.10/class-CakePlugin.html