Archive for December 2007

CakePHP: Creating a plugin.

Posted by: JDS

I created a sample plugin, "Hello World". Here it is, along with a tarball to test out.

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.
  1. Create a directory to house the plugin, named after the plugin.
    For "Hello World," for example, mkdir APP/plugins/hello_world
  2. Create a plugin app_controller
    For "Hello World," for example, vi APP/plugins/hello_world/hello_world_app_controller.php
  3. Create a plugin app_model
    For "Hello World," for example, vi APP/plugins/hello_world/hello_world_app_model.php
  4. 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
  5. 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

PHPgACL can be found at http://phpgacl.sourceforge.net/ CakePHP can be found at http://cakephp.org/
Text here

Using vendor libraries in CakePHP

Posted by: JDS

First a rant, and then some actual useful information.

<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.

Setting Up JanusVM

Posted by: JDS

In the interest of longevity, here are some instructions

LinuxMint is my new favorite Linux distro

Posted by: JDS

Well, not that new of a favorite: been using it about a year.

Why is LinuxMint my favorite? (Or should that be "favourite"?) This is a list of things that are improvements over its Ubuntu base. If it is already in Ubuntu, even if it kicks ass, I probably won't list it.

  • Based on Ubuntu, and all that entails.
  • Adobe Flash support out-of-the-box
  • Java and Java Browser Plugin support out-of-the-box
  • Multimedia codec support out-of-the-box. I.e. can play movies including .mov, .avi, .wmv, and more.
  • Envy included (easy easy easy NVIDIA driver installer)
  • Amarok included (Banshee, Rhythmbox suck by comparison)
  • "Open in Terminal" in Nautilus
  • Looks great (subjective, yes)
  • Great community -- you have Ubuntu community + LinuxMint community!
  • Great developers who provide strong encouragement to the community (as compared to, say, MEPIS, which, although it was my previous favorite distro, is too one-man-runs-the-show for my tastes.)

LinuxMint's tagline should be "Better Than Ubuntu". But the "freedom/elegance" thing is pretty good, too.

LinuxMint Daryna new installation notes

Posted by: JDS

Upgraded the wife's computer. Mostly fine, but with hangups.
Problems (P) and Solutions (S):
Problem
Opera sucked up memory (or something) like a starving tennessee hog on home grown grits.
Solution
Something is wrong with Gutsy's build of Opera. Perhaps. Not sure what is wrong, exactly, but it was (sorta) confirmed by at least one other person on their blog. In any case, memory usage was high (but not, as it turned out, abnormally or astronomically high) and, more importantly, CPU was at 100% and disk churned (swap space?) when even only a few tabs were open. Overall performance of the computer dropped to unusable. I (1) Uninstalled Opera via Synaptic and (2) installed the static binary from Opera.com ("Select distribution and vendor->Other/Static DEB"). Static binary! I figured they probably put together an okay build for themselves, no sense in messing about with possibly unstable Ubuntu libs.

In fact, this problem was so bad, it really warrants being listed as a bug in Launchpad. We'll see if I get around to that...
Problem
"General input/output error" when saving OpenOffice docs to our NFS shares.
Solution
Add "nolock" to NFS mount options in /etc/fstab. For example, server-hostname:/home/public /server/Server_Public nfs exec,rw,intr,user,noauto,nolock,soft,timeo=1 1 1
Apparently, OpenOffice has some sort of problem with NFS shared stuff. NFS locking. Anyway, I found this listed all over the place, and the possible solutions were pretty much

(1) Turn off NFS locking in the OpenOffice startup script by commenting out SAL_ENABLE_FILE_LOCKING=1 in the "soffice" startup script. This did not work for me.
(2) Use NFS v4 on the server (not sure what that is, exactly, or how to tell what version I am currently using) but this was not an option for me because I cannot upgrade the server for a variety of reasons.
(3) Add the "nolock" option to the line in /etc/fstab. This did work for me.
Problem
OpenOffice icons missing! Toolbar has text ONLY. Yuck!
Solution
Turns out that Ubuntu (and by ancestry, LinuxMint), while it has several available icon themes for the OpenOffice toolbars, only installs one of these icon themes by default! So, if you change appearance in Gnome, the icon theme for OpenOffice may be missing! Install the other icon thems for OpenOffice; problem solved. These packages are called openoffice.org-style-* where * is "human", "tango", etc.