KB Knowledge Base
CodeIgniter : Understanding The Basics With A VPS

Article By daniel Dhar



codeIgniter VPS Cloud Hosting Virtual Private Server - BIP media As technology moves forward and becomes the domain of the common individual, Object Oriented Programming has also gained a considerable amount of popularity. CodeIgniter is a great example of a program gaining such popularity, and to make it even better, it is a lightweight PHP web application framework. What does that mean for you exactly? Because CodeIgniter IS in fact a PHP application, it does not necessarily need to be installed on your personal/local machine. Instead, it will be installed on a server that utilizes a PHP plugin, meaning it can be used remotely or locally, depending upon the level of skill you have as well as your preferences.

In addition to using a Model-View-Controller for better presentation, CodeIgniter uses Clean URLs, meaning you won’t have to worry about those disaster URLs from the 90’s. All of this, of course, depends upon your ability to install the program. Most tutorials would work under the assumption that you have already installed the program, but we are going to cover all the bases so that you don’t have to hunt for installation tutorials around the web.

Installing CodeIgniter

Before you can proceed with the installation you will need to start by downloading the proper installation package which can be found at http://www.codeigniter.com/ . Once you have the software, you will notice that it comes in a .zip package, and fortunately, Windows comes with the software you need to extract the files within. Once you extract the files, make sure that they are in an easy to remember location for the first step. After the package is unzipped, start by dragging the CodeIgniter folders to your server. Make sure you place them at your root. Next you will need to make some changes to the config file by opening application/config/config.php. Do this with your favorite text editor, and if you are using a command line, you will want to utilize either Pico or Nano. Inside the document, you will want to set your base URL(or IP address if you have no URL) and set your encryption key. In the event you want to use a database, use your text editor to open application/config/database.php and configure your database settings. Because this is a web based application you will be able to run it on Windows, Mac, Linux, or UNIX. If you need to run the application the go, you will even have the option of using it from your Android device. CodeIgnite is a powerful, highly versatile piece of software that you can use anywhere.

Using the MVC (Model-View Controller)

MVC programming separates presentation from logic which is very helpful when you have both developers and programmers working on the same application. Models and Controllers are PHP classes that you must declare, and they are kept in separate files. Views, in contrast, are just HTML pages that contain very little PHP code. The view is where information is presented by the server, which generally means the webpage that most people will see when they navigate to the domain int heir web browser. The controller routes the request from the client, then uses the Model to load the needed views. It is a very simple three part process.

Routing the Data

Moving on from the concept of the Controller, we must now ask the question of how information from the three part process is called in by the client. The client of course is a browser capable of displaying the information, such as Firefox or Internet Explorer. How does: it handle URLS exactly? The tutorial before this one mentioned URL configuration, and from it we learned that the URL is built like this:


This structure ensure that after the base URL, the class will be initiated followed by the designated method. Finally, the arguments used to invoke the function will be taken into account. Now, if you require more arguments, they will be separated by another forward slash. If you are interested in breaking away from the standard then you have several different options. For this example we are going to assume you have a simple application, the frontend using a single controller and method. You have a two choices when it comes to your configuration here:

Choice 1: Set up your site so that the controller method is the default function called when the user navigates to the webpage via their browser.

Choice 2: Create a set of rules by which whatever is passed following the base URL is a representation of the arguments. If you want to change any of this, you will need to open the following: Application/config/route.php The following assumes that you have the default CodeIgniter installation, meaning you have not made any personal changes to the config file. That said, in this file you should see the following line:

$route['default_controller'] = "welcome";

This is the default setting, and it will be used if nothing is passed over the base URL. You willw ant to change this to reflect your controller:

$route['default_controller'] = "your_controller";

This is a good start, but now, to ensure that the proper method is called, change the line to:

$route['default_controller'] = "your_controller/your_method";

Once this is done, your method will be called by default. This could be the end of it, but we do need to add some more lines to the file. Right below your new line you will need to write the following:

$route['(:any)'] = 'your_controller/your_method/$1';

This line will ensure that anything added after the base URL will not be considered a Controller, but rather an argument. You may want to add a comment below this line to ensure you remember. Once you have added this line, you will want to save and test the file. If it does what you want, then congratulations, now start playing around with the different variations!

Libraries and Helpers

Within the CodeIgniter software you will find a number of different libraries, all tasked with making your development a lot easier. These, however, are not loaded into the software from the beginning. To the raw beginner this might seem a bit inconvenient, but it actually keeps the software from becoming bloated. As you need these libraries, you will load them yourself.

Adding the Database Library/Class

 If you want to add the Database Library into your controller, you will need to load it using the following line in your method:

Nevertheless, you also have the option to autoload libraries and helpers. This means that the application will include them from the beginning so you don’t have to bother with loading them individually in various Controllers. Autoloading Libraries and Helpers If you do not want to do it manually, you will want to investigate the different methods of autoloading, and we will start by discussing the database class and URL helper. Because you will use these quite often anyway, it only makes sense to autoload them. That being said, you will want to open the following file:

Application/config/autoload.php Inside this file you should find the following line:

$autoload['libraries'] = array();

 This is actually a rather easy fix and you will only need to add one word, or if you wish, copy and paste the following line to replace:
$autoload['libraries'] = array('database');

The array, obviously, must contain the class names that you want autoloaded. In tis next example let’s assume youw ant to autoload the helpers:
$autoload['helper'] = array('url');

See how easy that is?

Save the file, load the application, and your libraries/helpers will be autoloaded. This will make your life easier, and with CodeIgnite, your life will only be as easy as you need it to be.

Tags: , , ,

Spin up a VPS server in no time flat

Simple setup. Full root access. Straightforward pricing.


Leave a Reply