Building a Barebones Content Management System: An Introduction - Setting it up (Page 4 of 5 )
After setting up the database in the previous section, I proceed to configure yaapi itself. For starters, I need to configure obvious parameters such as the database server, the database name, and so on, so that yaapi can retrieve information from the database that I’ve just created above.
No sweat; locate a file titled "article.class.php" in the root folder of the application and search for the following code snippet:
<?php
// snip
/**
* object constructor
*/
function article() {
$this->version = '1.2.1';
$this->auto_approve_insert = 0; //bool
$this->auto_approve_update = 0; //bool
$this->show_approved_only = 1; //bool
$this->show_section_only = 1; //bool
$this->hide_intro = 0; //bool
$this->use_fix_date = 1; //bool
$this->section_id = 0; //int
$this->word_wrap = 0; //width, 0 disables
$this->allowed_tags =
'<a><b><i><u><ol><ul><li><br><p><blockquote>'; //string
$this->intro_delim = '#intro#';
$this->page_delim = '#page#';
$this->a_table = 'article';
$this->ac_table = 'article_content';
$this->cat_table = 'article_category';
@$this->link_id = mysql_connect('localhost', 'bb_cmsuser', 'password_comes_here') or die('Could not connect');
@mysql_select_db('bb_cms') or die('Could not select DB');
}
// snip
?>
As the comment suggests, the above code snippet represents the constructor of the yaapi "article" class. For the uninitiated, the term "constructor" is just a fancy term for a function that gets things moving as far as objects are concerned. It’s obvious that this is where I can configure almost all aspects of the application; for the moment, let’s stick to the default values.
As you might have guessed, the next two of lines of code are of immediate interest as this is where the API initializes a connection to the database. Here I have to specify the server name (i.e. "localhost" in my case), the database username (i.e. "bb_cmsuser") and password (specified at the time of user creation) as well as the name of the database (i.e. "bb_cms") that I intend to use.
However, I was not comfortable with the idea of specifying these parameters in this file. After all, it will be cumbersome to modify these values when you move your application to a different server. So, I decided to develop a quick work around for this problem by creating a separate configuration file -- titled "inc.config.php" -- to store my instance specific information.
<?php
/**
* Configuration file
*
*/
/**
* Database parameters
*/
$DBHOST = "localhost";
$DBNAME = "bb_cms";
$DBUSER = "bb_cms_user";
$DBPASS = "password_comes_here";
?>
Next, I update the "article.class.php" file to include this configuration, to ensure that I can access these parameters via the $GLOBALS variable, as demonstrated below.
<?php
// Core library and configuration files
require_once("config.inc.php");
// snip
@$this->link_id = mysql_connect($GLOBALS["DBHOST"], $GLOBALS["DBUSER"], $GLOBALS["DBPASS"]) or die('Could not connect');
@mysql_select_db($GLOBALS["DBNAME"]) or die('Could not select DB');
// snip
?>
That’s much better, isn’t it? Time to move on.
Next: First Glance >>
More Administration Articles
More By Harish Kamath