Bake a Plugin
To bake a plugin for Croogo, go to Croogo’s app folder in terminal and type following command. Let’s create a plugin named Library.
$ cd ~/public_html/croogo $ Console/cake bake plugin Library
You will be asked to choose the location of the plugin. Select outermost
Plugin/ folder. Above command does the following things:
- Creates proper directory structure for new plugin
- Creates plugin’s AppController.php file (
- Creates plugin’s AppModel.php file (
- Adds entry in
Config/bootstrap.phpfile to load newly created plugin
It is recommended that you delete entry of newly baked plugin from
Config/bootstrap.php file as you will be able to enable-disable plugin from UI. The line will look something like:
CakePlugin::load('Library', array('bootstrap' => false, 'routes' => false));
Configure Plugin to enable – disable
To make a plugin available to be enabled – disabled from the admin user interface, you will need to edit the plugin.json file. This file will not be present in the newly created plugin so you can copy this file from Example plugin already present in Croogo.
$ cd ~/public_html/croogo $ cp Vendor/croogo/croogo/Example/Config/plugin.json Plugin/Library/Config/
And edit this config file with the information of your plugin. After creating this file, you should be able to enable-disable plugin.
Add Menu for Your Plugin
Croogo has a
CroogoNav() class which is used to add and display menus. It has static functions
CroogoNav::items() that you can call from anywhere in the app. For our plugin, we will add entries for a new menu in plugin’s bootstrap.php file. If this file is not present in your Plugin’s Config folder, you can create a new
Plugin/Library/Config/bootstrap.php file and write entries for menu there.
Bake Controllers, Models and Views
Before we begin to bake controller, model, and view for our plugin, let’s first understand the structure of our plugin. We are trying to create a simple plugin where we should be able to record entries of Books and their Authors.
We will create two tables, one for each
Authors and then will create their controllers, models, and views. If you understand CakePHP associations – Book belongsTo Author and Author hasMany Books.
CREATE TABLE IF NOT EXISTS `books` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `description` TEXT DEFAULT NULL, `author_id` int(11) NOT NULL, `created` datetime DEFAULT NULL, `created_by` int(20) DEFAULT NULL, `updated` datetime DEFAULT NULL, `updated_by` int(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `authors` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `created` datetime DEFAULT NULL, `created_by` int(20) DEFAULT NULL, `updated` datetime DEFAULT NULL, `updated_by` int(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Okay, let’s start baking them by using CakePHP bake utility.
$ Console/cake bake model -p Library Author $ Console/cake bake controller -p Library Authors --admin --public $ Console/cake bake view -p Library Authors $ Console/cake bake model -p Library Book $ Console/cake bake controller -p Library Books --admin --public $ Console/cake bake view -p Library Books
Congrats, you have now created your Croogo plugin. You can check the plugin from by logging in to admin. You can start further changes by directly editing files in your Croogo Plugin.
Much of the part is taken from the following video created by Rachman Chavik.
If you find this useful, please share. If you have any suggestions, questions please leave a comment.
Subscribe to Rakesh Tembhurne
Get the latest posts delivered right to your inbox