Wednesday, October 14, 2009

Plugin creation in 10 mintue, #cakephp, #php, #openSource, #MVC

Do you want to create a blog, a web management module, or anything you want for an application? Make it a CakePHP plugin and you can also put it in other applications. The main tie between a plugin and the application it has been installed into is the application's configuration (database connection etc.). Otherwise, it operates in its own little space, behaving much like it would if it were an application on its own.

To start, place your plugin files

in /app/plugins folder. Choose wisely the name of the folder for the plugin files because it will be used in many places. For any normal application, you do not need to define AppController and AppModel; that's required for the plugins. You'll need to create them before your plugin works. These two special classes are named after the plugin, and extend the parent application's AppController and AppModel. For example, the AppController will be /app/plugins/softpedia_test_controller.php and the AppModel /app/plugins/softpedia_test_model.php. If you forgot to define these classes, CakePHP will show you "Missing Controllers" error until you solve the problem.

The controllers for the plugin will be stored in /app/plugins/name_of_plugin/controller. In order to avoid namespace conflict, you should give the controller a unique name. The models of the plugin are stored in /app/plugins/name_of_plugin/models. Next, you will specify the place where you will put the plugin views. Views for the plugin have the same functions as in normal applications. Place the views in /app/plugins/name_of_plugin/views folder.

Before starting to create your CakePHP application, you should consider the following conventions:
- model filenames are singular, capitalized for single-word models, and UpperCamelCased for multi-word models.Also model filenames use a lower-case underscored syntax: e.g: softpedia_test.php
- table names are plural and lowercased
- foreign keys should always be: table_name_in_singular_form_id: user_id (foreign key) �� users (table)
- controller filenames are plural and underscored with "controller" appended: controller_name_controller.php

You can find all the conventions in the manual.

No comments:

Post a Comment