Blog Categories

Recent Articles

Popular Articles

Latest Tweets

  • Getting Started with CodeIgniter
  • Not too long ago, I started working with CodeIgniter, a framework created by the same people that brought us Expression Engine.  Part of the reason as to chose this framework was due to the fact that it was lightweight and was built for performance.  Something that I felt ADDT was lacking in sometimes.

    In this tutorial, I will showing you how to setup CodeIgniter on your system, as well as a small insight as to a setup that feel might be ideal for your applications.  First of all, let's start off by downloading CodeIgniter from http://www.codeigniter.com/download.php.  The latest release (dated on the 11th Sept) has some changes and well as support for PHP 5.3.

    Extract the download archive file in your htdocs folder of your XAMPP installation.  (If you don't have XAMPP installed, please feel free to follow my tutorial on how to install your own XAMPP server here ).  Once the zip file has been extracted, you should see a structure similar to the one below.

    Now if we type in http://localhost/codeigniter_1.7.2/ in our browser address bar, we should get a welcome message confirming that CodeIgniter has been successfully setup.  We can also get the same page by typing in http://localhost/codeigniter_1.7.2/index.php/welcome

    Now that we know, our CI setup is running properly, we need to think of securing the application.  There are several methods that you apply here.  If you have your application hosted on a Linux server, you could move the system folder to the server root and keep the application folder in a public_html folder.   If find it easier to move the application folder out of the system folder, rename the system folder to something else and finally give the application folder another name.  Let's try that shall we.

    Click on the application folder and move it just above the system folder.  Rename the application folder your application name.  In this example, we will use my_ci_app.  When it comes to renaming the system folder, I find it best to rename the folder according to the version of CI you have installed.  This technique came in pretty hand when I was working with version 1.7.1 and realised that it would not work with PHP 5.3.0.  Once I download the SVN version (which was 1.7.2), I was not willing to overwrite the original folder.  So I just had 2 different system folder containing different versions of CodeIgniter.  Anyway, we will just rename our system folder to ci_v172.

    You should now have a structure that looks like the one in the image below.

    Now if try to view the CodeIgniter site again, we will simply get a bunch of errors.

    Ugh! Not good. We need to fix this.

    Open up the index.php file located at the room of your CodeIgniter application.

    Using your editor of choice, open up the config.php file and locate the line that says:

    $system_folder = "system";

    Change it to:

    $system_folder = "ci_v172";

    Next locate the line that says:

    $application_folder = "application";

    Change it to:

    $application_folder = "my_ci_app";

    Finally, save the index.php and refresh the browser.  Our CodeIgniter application should now be working again.

     

    Setting the base URL

    Everytime I started a new CI application, I had to set the base_url of the application manually.  Although this was fine, I started noticing a few problems when I had to move the application elsewhere.  Now if you are working with multiple CI applications, this can be quite tedious as you will have to peruse through every config file in your applications just to set the base urls manually.  Digging through the superb CodeIgniter forums, I came aross a little gem that should make any application portable.

    Open up the config.php file with your editor, locate the base_url configuration line.

    $config['base_url'] = "http://example.com/";

     And change it to:

    $config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
    $config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

    Now the above piece of code should make it easy for you to move your application around without having to tamper with the base_url manually everytime.

     

    Friendlier URLS

     By default, CodeIgniter allows us to have friendly URLs.  There is an option in the config file if you wish to use query strings.  The main issue that we have here however is that in other to load another controller, we have to enter something like http://localhost/codeigniter_1.7.2/index.php/[controller_name].  I personally would rather prefer not to have the index.php in the URL.  So in order to change this, we will need to create a .htaccess file at the root of our CI application.

    Using notepad or your editor of choice, create a .htaccess file.  Once you have created and opened the file, add these lines of code:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    

    We will also need to make a small change in our config file.  Locate the line below:

    $config['index_page'] = "index.php";

    Change it to:

    $config['index_page'] = "";

    So now that this done, we can now call any controller in our CI application without having to include the index.php file everytime.

    Also depending on which servers you have your CI application running on, you might find that the URI routing protocol might not be working as well as expected.  So you could change the setting from AUTO to either PATH_INFO, QUERY_STRING, REQUEST _URI or ORI_PATH_INFO. REQUEST_URI is generally the best option to take if you encounter problems.

    I hope this tutorial proves useful to those who are looking to use CodeIgniter for their projects but have no idea of where to start.  Remember, there is a user guide included in the CodeIgniter download file, so I will be a good idea to start there.  And oh, it's one of the best user guides I have ever seen that comes with framework.

    Let me know your thoughts.

     

  • Share Share Bookmark

« Previous Article Next Article »



Related Articles


blog comments powered by Disqus