Codeigniter Basics
What is CodeIgniter?
CodeIgniter is a ADF, or an Application Development Framework made for people who build web sites with PHP. Its main strength is its ability to make commonly needed tasks a breeze to perform. Some advantages of using CodeIgniter include:
- Its free.
- Lightweight and fast.
- It uses MVC.
- Generates clean URL’s.
- Thoroughly documented.
- User friendly.
By using an ADF, the creative process is not slowed down by semantic hiccups. You can connect and query your database with one line. Additionally, using a MVC approach, you can separate application logic from presentation. This permits your web pages to contain minimal scripting since the presentation is separate from the PHP scripting.
“Really fast. We challenge you to find a framework that has better performance then CodeIgniter.” - CodeIgniter User Guide
How it Works
Lets start by explaining how MVC works. The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database. The View is the information that is being presented on a page. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer commonly used in PHP templates. It can also be an RSS page, or any other type of “page”. And finally, the Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.
It is important to note that in CodeIgniter, does not require the use of Models.
Using CodeIgniter
Well before we begin working with CodeIgniter, you should probably download and install it. You should also have a low-level understanding of PHP.
Note: You will need to have at least PHP4 installed on your server that you are testing on.
Controllers
A Controller is simply a class file that is named in a way that can be associated with a URI.
Example URI:
www.your-site.com/index.php/blog
In the above example, CodeIgniter tries to find a Controller named blog.php and load it. When a Controller’s name matches the first segment of a URI, it will be loaded.
Creating the Controller
Create a file named blog.php, save to your Applications/Controllers and put this code into it:
class blog extends Controller {
function index(){
echo ‘Hello World!’;
}
To test this process, you can enter the URI from above (www.your-site.com/index.php/blog/).
If everything goes well, you should see Hello World! in your browser.
Defining Default Controller
If you don’t want to use a URI, you can set your default Controller by opening your application/config/routes.php file and setting this variable:
$route['default_controller'] = ‘Blog’;
Where Blog is the name of the Controller class you want used. If you now load your main index.php file without specifying any URI segments you’ll see your Hello World message by default.
You can find more information on controllers here.
Views
A View is just a page. It could also be a fragment such as a Header or a Sidebar View, commonly used in PHP templates. A crazy thing about Views is that they can be called within each other.
Note: Views are never called directly, they must be loaded by a controller.
Creating a View
Lets continue with the Blog Controller demo and create a View for it.
Create a file named blog-view.php, save to your Applications/Views and put this code inside the body tag of your HTML:
<h1>Welcome to my Blog</h2>
June 18th, 2007 at 11:27 pm
I’ve been using this framework for a while now and I must say… It’s nothing short of brilliant. The good thing about it is that it doesn’t get in your way.
July 10th, 2007 at 9:57 am
I’ve been using codeigniter for a while now. I’ve built two CMS systems in 4 days using it and had I been developing traditionally it’d have taken at least 3 to 4 times longer.
The documentation is great but the only thing that it lacks is advanced usage of various classes… but the discussion forums counter that
January 15th, 2008 at 7:30 pm
nice summary and a good amount of content.
Thanks for peaking my interest.
April 11th, 2008 at 12:02 am
I think that you really can judge people by the way they comment different stuff. Some people, even expressing negative thoughts, are still polite and they respect and understand other people. Some people are not even trying to be nice, they just don’t care. I think self-confident person will always act nice, no matter what other people do