Blog

21 Nov
2012

CodeIgniter Icon Logo

Should we be using CodeIgniter?


For me as a freelancer I would answer the question 'Should we be using CodeIgniter?' and say 'its dependent on the individual project at hand' or when I walk into a studio 'it depends on whats already been done!'. But either way, no php developer can deny the demand for MVC (Model View Controller) frameworks has gone up considerably, especially in the freelance world.

But if you are wondering if it's going to be a steep learning curve (which in all fairness is what I thought at first)... don't worry I suspect you are already coding your own custom MVC framework or similar. So for example all my custom websites I created used a simple structure like below:

index.php
class_function_folder/config.php
- System configuration file
class_function_folder/function_classes/ - Mix of System and Individual php
class_function_folder/layout_classes/ - Individual Page Layouts
css/
javascript/
images/
.htaccess

Compared to CodeIgniter (There are other folders but these are the main items needed.):

index.php
application/config/
- Various system configuration files
application/views/ - Individual Page Layouts
application/models/ - Class and functions
application/controllers/ - Control individual Page related elements such as views and models
system/
css/
javascript/
images/
.htaccess

This is a very basic comparison, but you will notice they are very similar in structure, and that's the clue. Obviously CodeIgniter has its own individual call functions, but genuinely it should take only 30 minutes to get your head around the basics.

So at first start by downloading CodeIgniter from https://ellislab.com/codeigniter/user-guide/installation/downloads.html.

After unpacking the code and setting it up using something like MAMP, I would suggest you create the .htaccess first thing and place it in the root directory using the following code:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ /index.php/$1 [L]

This is the most important thing in order to use a SEO friendly url structure, which is the basis of how CodeIgniter works, and in all fairness how your own custom php code should work.

By default you will see a welcome page (based on version CodeIgniter 2.1.3)

CodeIgniter Default Welcome Page

From the above image you can see that the html layout of the page sits in 'application/views/welcome_message.php'.

And all the page layouts and functions than need to be included (controller) that relate to that page reside in application/controllers/welcome.php'.

And all the custom functions (models) that relate to that page reside in application/models/'.

But you may ask yourself... how does the root index.php file know to make the welcome_message.php file the default page layout.

Well first check application/confiq/routes.php which contains the following setting '$route['default_controller'] = "welcome";'. This relates to two things the filename 'welcome.php' and the class name inside this file called 'class Welcome'. They have to share the same name thats important, so best to remember this when developing other pages. Also another thing to point out is that the class name must always start with an uppercase letter... so for this example 'class Welcome' works but 'class welcome' wouldn't.

Second open the application/controllers/welcome.php file and find the following:

public function index()
{
    $this->load->view('welcome_message');
}

With this you are outputting the view/html layout for the application/views/welcome_message.php file, but if you wanted you could add direct output via an echo like below.

public function index()
{
    echo 'Hello World!';
}

As you can see the structure and functionality of how CodeIgniter works is relatively easy to understand. Yes there are built in functions that need to be learnt, but as a freelancer I would say thats part of the job. Next I will be showing a more complex example of a simple login form.

Tagged: < basics | codeigniter | html | php | web >
Posted in: < PHP MVC >
me@grafxflow avatar

me@grafxflow


I am a Full-stack Developer who also started delving into the world of UX/UI Design a few years back. I blog and tweet to hopefully share a little bit of knowledge that can help others around the web. Thanks for stopping by!

Visitors also viewed these posts


Add comment

Add comment