Packages Tutorial

In this tutorial, we will create a basic PHP web app to view and manage the presidents database created in the Content Type Tutorial.

We will keep the PHP code very basic to only introduce COMAND PHP API features that query, retrieve and modify Presidents in the repository.  Subsequent tutorials will cover better coding practices and approaches, including MVC and webCOMAND Apps.

Create "Presidents Demo" Package

In webCOMAND, a web app can be created as a Package that contains all of the app's code and support files.  A package makes it easy to organize, access and share a collection of related files and objects.  So, the first thing we will do is create a new Package.

  1. Launch the Content Manager (CMS) App.
  2. In the folder tree on the left, expand and click: Bases / Presidents
  3. Click the Add Content menu (New Option) and select "Package" in the drop-down.
  4. Enter the Title "Presidents Demo"
  5. Click the Package tab and enter a unique Namespace for our app, such as "com_presidentsdemo_www".  Reverse domain name notation is not required, but is a best practice to ensure unique package folder names and namespaces.
  6. Click Approve.

Create "public/index.php" PHP File

Now that we have a package, we can add our first PHP web app file.

Files in a Package are mirrored in the file system.  So the following steps will create files/packages/com_presidentsdemo_www/public/index.php.
  1. Click the Contents tab in our new "Presidents Demo" Package.
  2. Click the New Menu (New Option) in the Contents field and select "PHP File" in the drop-down.
  3. Enter the Filename "public/index.php".
  4. Enter the Text:
    // connect to the default comand repository
    $repo = \comand::repo();
    // list presidents
    $presidents = $repo->get('FROM President ORDER BY Number');
    foreach($presidents as $p) {
        echo('<li value="' . $p->Number . '">' .
         '<a href="edit.php?oid=' . $p->OID . '">' . $p->Name . '</a>' .
  5. Click Approve and then Back.

Create "public/edit.php" PHP File

Now we can add the edit functionality referenced in the "public/index.php" file.

  1. Click the New Menu (New Option) in the Contents field and select "PHP File" in the drop-down.
  2. Enter the Filename "public/edit.php"
  3. Enter the Text:
    function get($key) {
        return isset($_REQUEST[$key]) ? $_REQUEST[$key] : NULL;
    function get_file($key) {
        return (isset($_FILES[$key]) && $_FILES[$key]['tmp_name'])
            ? file_get_contents($_FILES[$key]['tmp_name'])
            : NULL;
    function edit_president( $oid, $op ) {
        // connect to the default comand repository
        $repo = \comand::repo();
        // get and validate president from oid
        $p = $repo->get_object_by_oid($oid);
        if(!$p || $p->Type->Identifier != 'President') {
            return '<p>Invalid President OID.</p>';
        // if this is an update, get and submit form values
        if($op == 'update') {
            try {
                $p->Name = get('name');
                $p->Number = get('number');
                if($photo = get_file('photo')) {
                    $p->Photo = $photo;
            } catch( Exception $e ) {
                return '<p>Update Error: ' . $e->getMessage() . '</p>';
        $foid = $p->Type->get_field('Photo')->OID;
        $photo_url = "/com_webcomand/img/$oid/$foid/96x96/4";
        echo <<<FORM
    <form method="POST" enctype="multipart/form-data">
        <input type="hidden" name="op" value="update" />
        <input type="hidden" name="oid" value="$oid" />
        <input type="number" name="number" value="$p->Number" />
        <input type="text" name="name" value="$p->Name" />
        <img src="$photo_url" /> 
        <input type="file" name="photo" />
        <input type="submit" value="Update" />
        <title>Presidents Demo</title>
        <style type="text/css">
            label, input, img {display: block; margin: 4px 0;}
            input {margin-bottom 16px; padding: 4px;}
    <?php edit_president( get('oid'), get('op') ); ?>
  4. Click Approve and then Back.


Files in the public subfolder of a package can be accessed on the web by appending the package namespace and file path within the package's public folder to the end of the webCOMAND base URL.  If the filename is not specified at the end of the path, index.php will be discovered and served automatically, and if index.php is not found, index.html will be discovered and served if it exists.

So, you can now view and edit Presidents in the web application at: