webCOMAND

Dynamic Page Publication Tutorial

In this tutorial, we will create a dynamic web page that accesses the presidents database created in the Content Type Tutorial, at page request time instead of at publish time.

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 web application approaches, including MVC and webCOMAND Apps.

Create "DynamicPresidents" Publication Procedure

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

  1. Click the "Publication Procedures" tab under "Tutorials" in the folder tree in left panel.
  2. Click the New Button (New Menu) in the right panel's toolbar.
  3. Enter the Identifier "DynamicPresidents".
  4. Enter the Filename "dynamic.php".
  5. Enter the Script:
    #PLAINTEXT
    <html>
    <head>
        <title>Presidents</title>
    </head>
    <body>
        <h1>Presidents</h1>
        <ol>
    <?php
        require_once('/var/www/webcomand/comand.php');
    
        // 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 . '">' .
            $p->Name .
            '</li>');
        }
    ?>
        </ol>
    </body>
    </html>
    #ENDPLAINTEXT
  6. Click Approve.

Update and Publish "Presidents" Publication

We need to add the new Publication Procedure to our Publication and publish.

  1. Click "Tutorials" in the left panel, and select the "Presidents" publication.
  2. Select Publication Procedure "DynamicPresidents" in addition to "President" and "Presidents".
  3. Click Publish
  4. Click "dynamic.php" in the Notification Side Bar.

It will display the complete list Presidents based on the latest Approved content.

Conclusion

Dynamic PHP code can be used to pull in the latest content, without the need to republish.  To test out our example, update and Approve a change to the Presidents database and refresh the web page without republishing.  The updates will be reflected with the next page request after the change is Approved.