Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
CakePHP 2 Application Cookbook

You're reading from   CakePHP 2 Application Cookbook Over 60 useful recipes for rapid application development with the CakePHP framework.

Arrow left icon
Product type Paperback
Published in Aug 2014
Publisher
ISBN-13 9781782160083
Length 346 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (14) Chapters Close

Preface 1. Lightning Introduction FREE CHAPTER 2. Advanced Routing 3. HTTP Negotiation 4. API Strategies 5. Using Authentication 6. Model Layer 7. Search and Pagination 8. Events System 9. Creating Shells 10. View Templates 11. Unit Tests 12. Migrations Index

Including a plugin

One of the greatest benefits of using CakePHP is its extensibility. Using plugins, you can extend and enhance the core functionality, which provides you with even more cake than you bargained for!

In this recipe, we'll look at loading a plugin using DebugKit, the official development and debugging tool for the framework, as an example.

Getting ready

We'll need to have DebugKit included in the application file structure before we begin. There are a couple of ways to do this.

Git clone or submodule

If you use Git, you can create a clone of the code by executing the following command from your app/ directory on the command line:

$ git clone https://github.com/cakephp/debug_kit.git Plugin/DebugKit

If you already have your application under version control using Git, you can also add it as a submodule. Assuming that the base of your repository is the app/ folder, you would run the following command:

$ git submodule add https://github.com/cakephp/debug_kit.git app/Plugin/DebugKit

Using Composer

If you're using Composer as your dependency manager, you can simply include DebugKit as a plugin by adding the following code to your composer.json file:

{
  "require": {
    "cakephp/debug_kit": "2.2.*"
  }
}

After updating your composer.json file, simply update your application's dependencies using Composer.

Downloading files

An alternative method is to simply download the files and include them manually in your application from the following location:

https://github.com/cakephp/debug_kit/archive/master.zip

How to do it...

Perform the following steps:

  1. Add the following code to your bootstrap.php file, found in app/Config/:
    CakePlugin::load('DebugKit');
  2. Add the following code to your AppController in app/Controller/:
    public $components = array(
      'Session',
      'Auth',
      'DebugKit.Toolbar'
    );
  3. Open your core.php file in app/Config/, and check if the following configuration value is set to a value higher than 0:
    Configure::write('debug', 2);
  4. Now, load your application in your browser, and you'll find an icon in the top right-hand corner of the screen, as shown in the following screenshot:
    How to do it...
  5. Click on the icon to see various panels (shown in the following screenshots) with data related to the current request:
    How to do it...
    How to do it...
    How to do it...
    How to do it...

How it works...

When we call CakePlugin::load(), we request the framework to load the plugin by the given name, which also accepts an optional second argument, which is the configuration array for the plugin. We had already included the DebugKit/ directory in app/Plugin/, which is where plugins are located. You can change the location of where the plugins are loaded from using the path option in the plugin configuration array.

After loading the plugin, we included the Toolbar component in our AppController so it's available in all applications. This is a component included with the DebugKit plugin, which allows us to display a toolbar in the browser, thus providing a range of panels to introspect the application, the current request, SQL queries, and more. You'll notice that we included both the Session and Auth components here as well to propagate them in our application. The plugin itself is also extensible, allowing additional panels to be added. Once our plugin was loaded and the Toolbar component available, we made sure that the debug mode was enabled in our core.php file; we then navigated to the application in the browser to view the loaded toolbar.

Plugins may also load their own bootstrap.php and routes.php files to set up configurations and define routing settings. However, when a plugin is loaded, you must specify to load these configurations and settings. For example, if you had a Reports plugin that used these, you would load the plugin using the plugin configuration array as follows:

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

You can also load all plugins in your app/Plugin/ directory at once by calling CakePlugin::loadAll().

See also

  • For a complete overview of plugins in CakePHP, go to http://book.cakephp.org/2.0/en/plugins.html
  • The The Search plugin recipe in Chapter 7, Search and Pagination
  • The Generating a PDF and The AssetCompress plugin recipes in Chapter 10, View Templates
You have been reading a chapter from
CakePHP 2 Application Cookbook - Third Edition
Published in: Aug 2014
Publisher:
ISBN-13: 9781782160083
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image