CodeIgniter
From InstallationWiki
| Official Page |
| Project Documentation |
| Download |
|
Setting up a CodeIgniter Site
Setting up the CI package on your web server is easy. This tutorial explains what happens when you install the site, and which files will be created. Let's look at:
- What software you require for your development site
- Installing the CI files: a simple download and unzip operation
- The basic configuration of CI: what the folders are and how they are organized
- The initial controller and view that CI installs
- Some basic modifications to show how these work
Contents |
[edit] Prerequisites
CodeIgniter is very flexible. It will work equally well with PHP 4.3.2 and above, or PHP 5. Since the majority of ISPs still don't support PHP 5, this is useful, and keeps down the hosting costs.
You will also need a database. CI's online user guide says: "Supported databases are MySQL, MySQLi, MS SQL, Postgre, Oracle, SQLite, and ODBC."
In order to develop and test a dynamic website, you need a web server. Normally, you would develop and test your site on a local server, i.e., one that runs on your own machine (with the loopback address 127.0.0.1 or localhost) rather than on a remote site on the Internet. If you aren't familiar with the process of setting up a web server, it's easiest to install a package such as Xampplite, which installs Apache, PHP, and MySQL on to a Windows machine with minimum configuration by you. Alternatively, some versions of Windows come with their own web server.
It also helps to have a good PHP editor on your system. You can do it all on a text editor, but I find that the syntax highlighting feature of a good editor saves me from making lots of simple mistakes with unclosed brackets or mismatched quotation marks.
Once you've reached this far, it will take you two minutes to have CI running on your system.
[edit] Installing CodeIgniter
One thing you don't need is your credit card: CI is completely free!
Once your server is set up, go to the CodeIgniter site at http://www.codeigniter.com/ and download the latest version of the framework. Version 1.5.3, the latest, is only 737KB when zipped, so the download doesn't take that long
Unzip the folder, and install the CodeIgniter files in your web root folder. If you are using Xampplite, this is usually the htdocs folder within the Xampplite folder.
The CodeIgniter index.php file should be in the root directory. The root folder is the folder that you would point at if you navigated to the site in this case, by accessing http://127.0.0.1.
When these files are on your machine, you can access them in two ways:
- As a URL e.g.,
http://127.0.0.1
- Through the normal directory path: e.g.,
C:/xampplite/htdocs/index.php
You should be able to see the CI welcome screen by simply navigating to your URL with the browser. It's that simple! The welcome page tells you that what you are seeing is built by two files, a view and a controller.
[edit] Exploring the File Structure
Once you have installed the CI files, have a look at the new directories that have been created. Understanding what the different types of files do is critical.
Your root folder should now look something like the diagram below. If you've ever looked at Rails, this structure will look fairly familiar.
You can divide these folders into three groups:
- The ones you will populate (e.g., controllers, models, and views: all in the application folder). Apart from the welcome view and controller that you have just seen, these folders are empty.
- The files in the system folder are the system code for CI (system/libraries, system/codeigniter, system/drivers, etc.). You can read them, and alter them if you wish but don't do this until you understand how CI works. And if you alter the base code, remember that you may have to alter it again when you download an update of CodeIgniter, since the new version may overwrite your alterations. You may also find that the new code no longer works with your amendments.
- The ones that are half written already, but may need additions or changes (language, config, errors.) These folders are set to defaults, but you will need to alter your config files right away; so let's get that over with.
[edit] The Configuration File
The config folder contains a group of files that set basic configurations for your site. Open the config/config.php file and tell the site where to find itself. The first few lines of the file should say something like:
/* |------------------------------------------------ | Base Site URL |------------------------------------------------ | | URL to your Code Igniter root. Typically this | will be your base URL, WITH a trailing slash: | | http://www.your-site.com/ | */ $config['base_url'] = "http://127.0.0.1/"; /*
Notice how well CI files are commented!
Alter the values in quotes to match your own web root. If you have a problem, more detailed setup instructions are given in the online manual.
As a basic principle, use the config.php file to store information about your site rather than scattering it around your files. Firstly, it is easier to update if it's all in one place. Secondly, when you transfer your site from the development server to the production server, you'll only have to make only one set of changes. Lastly, many CI functions assume that certain information is to be found there.
There are other config files in the config folder, but you can safely leave them at their default settings for now.
[edit] Does it Work?
The easy way to see if your site is working is to navigate to it using your browser. Assuming you're running it in the root folder of a local server, type in http://127.0.0.1 and you should see this:
That means CI is up and running.
[edit] Summary
In this tutorial, we've seen how easy it is to install CI. Once you have a development web server set up, all you need to do is download the CI code, unzip it, and copy it over. Then, we looked quickly at the shape of the files we've installed and did some basic configuration, and there we are with a working CI site.
[edit] Source
The source of this content is Chapter 2: Two Minutes' Work: Setting up a CodeIgniter Site of CodeIgniter for Rapid PHP Application Development by David Upton (Packt Publishing, 2007).
