BIP KB:
CodeIgniter : Understanding The Basics With A VPS

Article By daniel Dhar


img

Introduction To CodeIgniter

codeIgniter VPS Server Hosting Virtual Private Server - BIP media As technology moves forward and becomes the domain of the typical individual, Object Oriented Programming has also gained a considerable amount of popularity. CodeIgniter is an excellent example of a program gaining such demand, 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 installation on your personal/local machine. Instead, it can be installed on a VPS server oron a dedicated 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 established 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 upzip the data, make sure that they are in an easy to remember the location for the first step. After unzipping the package, 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. CodeIgniter 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 kept in separate files. Views, in contrast, are just HTML pages that contain very little PHP code. 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. The controller routes the request from the client then uses the Model to load the needed views. It is a straightforward 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 data, such as Firefox or Internet Explorer. How does: it handle URLS correctly? The tutorial before this one mentioned URL configuration, and from it we learned that the URL is built like this:

http://www.example.com/[controller-class]/[controller-method]/[arguments]


This structure ensures that after the base URL, the class is initiated followed by the designated method. Finally, the arguments used to invoke the function is taken into account. Now, if you require more arguments in your file, separate them 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 two choices when it comes to your configuration here:

Option 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.

Option 2: Create a set of rules that pass pass anything 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 personnel changes to the config file. That said, in this file, you should see the following line:

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

The default setting  are used if nothing is modified. You will want to change this to reflect your controller:

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

Good start, but now, to ensure you call the proper method, change the line to:

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


Once completed, your process is called by default. We still 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 wish to, then congratulations, now start playing around with the different variations!

Libraries and Helpers

Within the CodeIgniter software, you will find many 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 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:
$this->load->database();


Nevertheless, you also have the option to autoload libraries and helpers. The application will include them from the beginning. 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 said, you will want to open the following file:

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

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


 Actually, this is 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, must contain the autoloaded class names. In this next example let’s assume you want 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 making your life a whole lot easier.

With CodeIgniter, 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.

DEPLOY VPS SERVER

Leave a Reply



Feedbacks