How to organize your application's basic information using the Stanford Web Application Toolkit

From Web Services Wiki

Jump to: navigation, search

Contents

Problem

You want to maintain consistency within your application by storing its name, URL, and base directory in a central location.

Solution

Use StanfordApp to organize your application-specific information.

Using a configuration file

Setting up a configuration file is one method of keeping track of your application's details. Create or edit a configuration file with the following fields.

---
info:
 
  # Basic application info
  name: My Application
  admin email: nobody@stanford.edu
  admin name: Admin Name
  base url: http://www.stanford.edu/group/my_group/my_app/
  base directory: /afs/ir.stanford.edu/group/my_group/my_app/

Simply pass the path to the configuration file to the constructor for StanfordApp to load the settings.

// Include StanfordApp
include_once("stanford.app.php");
 
// Initialize and load settings
$app = new StanfordApp("/path/to/config.yaml");
 
// Test
echo "This application's name is " . $app->get_name();

Manual configuration

Alternatively, use the methods below to configure StanfordApp without a configuration file.

// Include StanfordApp
include_once("stanford.app.php");
 
// Initialize StanfordApp object
$app = new StanfordApp();
 
// Set the name of the application
$app->set_name("My Application");
 
// Set the administrator's name and e-mail
$app->set_admin_name("Admin Name");
$app->set_admin_email("nobody@stanford.edu");
 
// Set the base URL
$app->set_base_url("http://www.stanford.edu/group/my_group/my_app/");
 
// Set the base directory
$app->set_base_dir("/afs/ir.stanford.edu/group/my_group/my_app/");

Retrieving your application's information

Use the functions below to retrieve your application's information.

// Name
echo $app->get_name();                     // My Application
 
// Administrator
echo $app->get_admin_name();               // Admin Name
echo $app->get_admin_email();              // nobody@stanford.edu
 
// Base URL
echo $app->get_base_url();                 // http://www.stanford.edu/group/my_group/my_app/
 
// Base directory
echo $app->get_base_dir();                 // /afs/ir.stanford.edu/group/my_group/my_app/

Constructing absolute paths and URLs

StanfordApp provides the functions path and url to simplify the process of constructing absolute paths and URLs.

// Get the base URL
echo $app->url();                          // http://www.stanford.edu/group/my_group/my_app/
 
// Append to the base URL
echo $app->url("about/index.php");         // http://www.stanford.edu/group/my_group/my_app/about/index.php
 
// Get the base directory
echo $app->path();                         // /afs/ir.stanford.edu/group/my_group/my_app/
 
// Append to the base directory to construct a path
echo $app->path("txt_files/my_file.txt");  // /afs/ir.stanford.edu/group/my_group/my_app/txt_files/my_file.txt
Personal tools