Archive for December 2007
CakePHP: Creating a plugin.
Posted by: JDS
I wanted to see how easy (or not) it would be to create a CakePHP plugin. This example is a simple "Hello World" plugin that uses no database and has the bare minimum of features to be considered an actual CakePHP plugin.
A plugin mimics the underlying MVC structure of Cake's main Application directories and files. So to create a plugin, you need to recreate a Cake PHP App's file and directory structure as follows:
Some Notes For These Examples
- "APP" in these examples refers to your CakePHP APP directory
- My examples are for CakePHP 1.2. Therefore, the examples in the CakePHP Manual are not exactly the same as these examples. For example, Views in CakePHP use the ".ctp" extension.
- I develop, test, and deploy on Linux systems. So my examples use bash shell commands, such as "mkdir".
- I use the Vi text editor. Vim, actually. Well, gvim, to be absolutely precise. Change "vi" in the examples to whatever text editor you want.
-
Create a directory to house the plugin, named after the plugin.
For "Hello World," for example,mkdir APP/plugins/hello_world -
Create a plugin app_controller
For "Hello World," for example,vi APP/plugins/hello_world/hello_world_app_controller.php -
Create a plugin app_model
For "Hello World," for example,vi APP/plugins/hello_world/hello_world_app_model.php -
Create the plugin MVC directories
For "Hello World," for example,mkdir APP/plugins/hello_world/models
mkdir APP/plugins/hello_world/views
mkdir APP/plugins/hello_world/controllers -
And finally, create the PHP files that correspond to the Model, View(s), and Controller
For "Hello World," for example,vi APP/plugins/hello_world/models/hello_world.php
vi APP/plugins/hello_world/views/hello_world_controller.php
vi APP/plugins/hello_world/controllers/hello_world.ctp
Here is the code for the PHP and Template (i.e. View) files, above:
APP/plugins/hello_world/hello_world_app_controller.php
Note that I added the beforeFilter() function here. It is optional! But I needed it in my app to override some default beforeFilter() authentication checking that happened earlier in the code stack.
<?php
class HelloWorldAppController extends AppController {
function beforeFilter(){
return true;
}
}
?>
APP/plugins/hello_world/hello_world_app_model.php
<?php
class HelloWorldAppModel extends AppModel {
}
?>
APP/plugins/hello_world/models/hello_world.php
<?php
class HelloWorld extends HelloWorldAppModel
{
var $name = 'HelloWorld';
var $useTable = false;
}
?>
APP/plugins/hello_world/views/hello_world_controller.php
<?php
class HelloWorldController extends HelloWorldAppController
{
function index() {
$this->set('out', 'Hello World');
}
}
?>
APP/plugins/hello_world/controllers/hello_world.ctp
<?php echo $out ?>
Integrating PHP Generic ACLs with CakePHP 1.2
Posted by: JDS
Using vendor libraries in CakePHP
Posted by: JDS
<soapbox>There is no clear documentation on using vendor() in a CakePHP app. Arrgh! I had to piece this together from blogs, forums, tutorials, and other bits and pieces on the web. Why can't this very basic information be included somewhere on the CakePHP panoply of websites? I mean, in the "manual", vendors/ directory is mentioned, vendor() function is mentioned, but some very basic stuff is ommitted! Like all the stuff I list here.</soapbox>
Note: The examples assume the use of a vendor class called "HelloWorld", in a file "APP/vendors/hello_world.php", which looks like this:
<?php
class HelloWorld {
function HelloWorld(){
return "Hello World";
}
}
?>
Anyway, here are some very important basics to using vendor libraries in CakePHP.
Where do you use the vendor() function?
If you want to use the vendor libs inside a particular "action" (i.e. CakePHP "URL" or, more specifically, a method (function) inside a CakePHP Controller class), then call vendor() inside the "action".
For Example:
<?php
class MyClass extends AppController{
function HelloWorld(){
vendor( 'hello_world' );
$hw = new HelloWorld;
$hw->HelloWorld();
}
}
?>
You don't, however, use vendor() inside the class itself, like, say, outside class functions. Example:
<?php
class MyClass extends AppController{
// WRONG! You cannot call functions in this area of a class declaration!
vendor( 'hello_world' );
function HelloWorld(){
$hw = new HelloWorld;
$hw->HelloWorld();
}
}
?>
If you want to include the vendor libraries or plugins or whatever in a global context, for use by every controller, you can call vendor() in one of several different places. I am not sure which of these is best, though! For performance or security reasons, there may be a preferred location. The places are:
- Inside beforeFilter() in app_controller()
- Outside the class declaration in the controller file itself.
- In the "bootstrap" file APP/config/bootstrap.php
More information needed? Maybe. I'll try to get some more as I work with these things.