Packages
In this tutorial, we will create a basic PHP web application to view and manage the presidents database created in the Content Type Tutorial.
Create "Presidents Demo" Package
In webCOMAND, a web application can be created as a Package that contains all of the application'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.
- Click the Tutorials folder in the folder tree on the left.
- Click the New Menu (
) and select "Package" in the drop-down.
- Enter the Title "Presidents Demo"
- 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.
- Click Approve.
Create "public/index.php" PHP File
Now that we have a package, we can add our first PHP web application file.
files/packages/com_presidentsdemo_www/public/index.php
.- Click the Contents tab in our new "Presidents Demo" Package.
- Click the New Menu (
) in the Contents field and select "PHP File" in the drop-down.
- Enter the Filename "public/index.php".
- Enter the Text:
<?php // connect to the default comand repository $repo = \comand::repo(); // list presidents $presidents = $repo->get('FROM President ORDER BY Number'); echo('<ol>'); foreach($presidents as $p) { echo('<li value="' . $p->Number . '">' . '<a href="edit.php?oid=' . $p->OID . '">' . $p->Name . '</a>' . '</li>'); } echo('</ol>');
- 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.
- Click the New Menu (
) in the Contents field and select "PHP File" in the drop-down.
- Enter the Filename "public/edit/index.php"
- Enter the Text:
<?php 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; } $p->approve(); } 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" /> <label>Number</label> <input type="number" name="number" value="$p->Number" /> <label>Name</label> <input type="text" name="name" value="$p->Name" /> <label>Photo</label> <img src="$photo_url" /> <input type="file" name="photo" /> <input type="submit" value="Update" /> </form> FORM; } ?> <html> <head> <title>Presidents Demo</title> <style type="text/css"> label, input, img {display: block; margin: 4px 0;} input {margin-bottom 16px; padding: 4px;} </style> </head> <body> <?php edit_president( get('oid'), get('op') ); ?> </body> </html>
- Click Approve and then Back.
Conclusion
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:
https://<account>.webcomand.com/com_presidentsdemo_www/