Getting Started With CodeIgniter - 2

by Liz Jamieson on November 7, 2008

in Code Igniter, php

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Getting The Environment Set Up

Last time, we installed the software and altered the config.php file by adding two lines (one commented out). Please review Getting Started With CodeIgniter - 1 for details.

If you now visit the home page running locally on your PC, you should see this. Obviously you must substitute your root directory where I have used chaingang3.

Setting up database.php

Most web sites demanding the attentions of CodeIgniter will need a database. I am using MySQL. I created a MySQL database called chaingang3 and set up a username and password for it.

To communicate this to the CI system, edit the database.php file located in the config directory (same place as the config.php file was located).

Add the following lines to database.php, but of course use your own username, password and database name.  The placeholders for these parameters are already in the file, you just have to fill in the values.

$db['default']['hostname'] = “localhost”;
$db['default']['username'] = “myusername”;
$db['default']['password'] = “mypassword”;
$db['default']['database'] = “chaingang3″;

Setting Up constants.php

I like to place any constants that I need to have available throughout the web site in the constants.php file. So early in your development you won’t know what these are for the mostpart, but each time you need a new global constant, set it up in here. To start with I will put in the Version number of the site I am developing. This is the site’s 3rd version so I’ll make a suitable constant, and add it to constants.php.

define(’WEBSITE_VERSION’, ‘3.0′);

Setting Up Custom Functions

As you code, you’ll need to write PHP functions.  There is a special place to put these in CI. Go to system/application/helpers and create a custom helper PHP file with a name like mycustomfunctions_helpers.php.

For example, one function I always add to my custom helper file is something to alert PHP values without having to write the Javascript each time. This is symptomatic of my never using a debugger.  Here it is :

As time goes on, you’ll add more of your own functions to this file.

Sub Directories for Image, CSS, Javascript and Other Files

I put all the images that are part of the web design into a sub directory called site-images and all content images into a sub-directory called images. Further, I like to place all my CSS files into a css sub-directory and Javascript files into a sub-directory called scripts. I follow the same regime for any other special files the web site may need  - like say for example a sub-directory for flash movies, another for PDFs and so on.

To do this, create all your required sub-directories under the CI application directory in a new sub-directory called assets.  Of course you don’t have to call it assets - I do because someone on the CI forums suggested the name. Anyway - I set the following structure up and so I’ll always have somewhere specific to place required files.

  • Digg
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Blogosphere News
  • Blogsvine
  • Facebook
  • TwitThis

{ 0 comments }

Getting Started With CodeIgniter - 1

by Liz Jamieson on November 6, 2008

in Code Igniter, php

Installing CodeIgniter - a PHP Framework - On Your PC

First, be aware, I am not a CodeIgniter expert. I am a relatively new user and am just trying to find my way around it.  This series of posts is to give you a blow by blow account of how I am putting a CodeIgniter web site together, in real-ish time. If I succeed it will mean anyone can do this.

If you are new to PHP and want to get a head start, but think a framework is just too difficult, I am going to attempt to get you started. (Famous last words).  For a real CodeIgniter experts visit the CI forums.

CodeIgniter has to be the easiest PHP framework to install.  This is why it is my framework of choice. I am going to explain how to set it up locally to run on a PC. Before you start you are going to need the following:

  • PHP installed on your local computer and on your web server (PHP 4.3.2 at least), and a database, (MySQL 4.1 at least). There is more information in the user guide about support for other databases.
  • A PHP/HTML editor. I either WeBuilder. There are other arguably better development environments, but I lose patience with the difficulty to install them, and the mass of software to install to make the run in debug mode. This means I tend to debug in my head and with various output statements. Stone Age I know, but it hasn’t caused me any problems so far.
  • A root directory where your local web server expects to find sites you run locally.

My local web server root directory is : C:\Users\Lizzy\Documents\websites and this is where my local installation of Apache has its root directory set.
Code Igniter Directory Structure

  1. Download the latest version of CodeIgniter from the CodeIgniter Web Site. At the time of writing this was version 1.7.0
  2. Decide on a name for the new web site you are about to create using CodeIgniter. In my example this will be chaingang3, but obviously you will choose your own, and substitute that where I have chaingang3.
  3. Unzip the entire contents of the CodeIgniter zip file into local root directory (as specified in your httpd.conf file if you are running Apache). For example in my httpd.conf file, DocumentRoot is “C:\Users\liz\Documents\Websites” so I unzipped the CodeIgniter installation to that location.
  4. Once unzipped, rename the CodeIgniter_1.7.0 directory to the name of your web site. In my case I renamed it to chaingang3.
  5. You’ll end up with a directory structure looking like the image on the right within your root directory, wherever yours is.
  6. Next you have to configure CodeIgniter. This is easy to do. Open up the config directory that you can see within the application directory and open the file called config.php

Changes to Config.php

config.php locationThe only changes you need to make to this file (to begin with) are are shown here in the Base Site URL section. You’ll find config.php in the directory as shown by the pink arrow on the right.

You just specify the starting position of the site on your local machine, and also on its ultimate server destination.

Base_URL

Note that I put two base_url values in so that I can easily comment one out depending on where the code is running - either locally or on the web server.

$config['base_url'] = “http://localhost//chaingang3/”;
//$config['base_url'] = “http://www.chaingang3.com”;

OK - that’s it for now. I have to drive to Exeter to meet my husband on the train.

Next post will be getting the CSS and other supporting directories in, the database and getting the system to run.

  • Digg
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Blogosphere News
  • Blogsvine
  • Facebook
  • TwitThis

{ 1 comment }