Hello everyone, and welcome to the first CodeIgniter tutorial.
What will you learn?
1. What is a PHP Framework?
2. What is CodeIgniter?
3. What is MVC?
4. MVC Pattern
5. Why CodeIgniter?
What is a PHP Framework?
First of all, what is PHP? I’d say that PHP (Hypertext Preprocessor) is the best scripting language at this moment, even if is exactly 15 years old. PHP is able to create dynamic webpages interpreted by a webserver (Apache, Lighttpd, nginx, etc), but also can create GUI applications.
This is enough for you right now, if you need more details check these links:
If you already know what is PHP we have to move on. If you’re already a PHP programmer, I think you know that some parts of your code are duplicated. So this is the moment to find out what is a PHP Framework.
A PHP Framework is a collection of pre-written methods and classes, that are already secured, optimized, well documented and ready to use anytime, “out of the box”.
What is CodeIgniter?
As you maybe think, CodeIgniter is a PHP Framework for PHP 4 and PHP 5 in the same time. CodeIgniter’s main goal is to allow user to develop lightning fast applications, without writing your code from zero. So you already have a basic package, with everything what’s needed to write a kick-ass application.
What is MVC?
MVC (Model-View-Controller), is an architectural pattern used in almost all programming and scripting languages, including PHP.
The Model – usually is used to connect with the database, making CRUD operations (Create, Read, Update, Delete)
The View – most of the time, here you will print the data received from your controller, and other HTML markup. Basically, a view is every page you see over the internet.
The Controller – is responsible to handle the requests, manipulating models or other controller. Try to keep the controller as simple as possible.
So, let me explain it a bit. There would be 5 steps:
1. Your computer will send a request to the Controller
2. The Controller will interact with the Model, making a demand.
3. The Model will make the processing, then return some data to the Controller.
4. The Controller will analyze the results (maybe needs some more data, and it will make another request to another Model).
5. Finally, the data will be sent to the view, which is interpreted by the webserver, and will be displayed back in your browser.
Let’s take an example over a registration page.
1. You enter on the registration page, complete the fields and click Submit.
2. The request is send to the controller, it will ask the model about your identity.
3. The model analyze the data you sent (if you are already a member, if your data is correct, etc.) and it will send an Accept or a Denial back to the controller.
4. The Controller will analyze the Accept/Denial from the model, and will decide what to do.
5. Finally, it will send “a Welcome” to the view, which will be visible to you as a Welcome page, or an error page.
First of all, CodeIgniter has the MVC architecture. So your application will be organized as I’ve already explained.
Is very easy to learn, as it’s deeply documented and very easy to extend.
CodeIgniter is a very light PHP framework (2.1 MB including the entire documentation) compared with:
1. Zend Framework 1.10.2 Full – 24 MB
2. symfony 1.4.3 – 5.1 MB
3. CakePHP 1.2.6 – 4.5 MB
Is lightning fast compared with other PHP Framework, I think it’s the fastest PHP framework.
The installation process is very very easy, you only need to unpack latest stable version, upload it on your host ,simply set ONE parameter and you are done. If course if you need something more, you have a few config variables. We’ll talk about the installation is a future tutorial.
CodeIgniter doesn’t have a command line tool like CakePHP or symfony. Maybe some users will like the command line tool, because it will generate your models, views and controller and other stuff, but honestly I feel I am losing the control of such an application. Not everytime you will need a model or a view for your controller, so why generate them?
Maybe you were wondering “Why other frameworks are so big?”. Most of the frameworks have included in their core additional libraries (authentication, captcha, currency, feed, ajax, connection with webservices – Twitter, Flickr etc.), Object Relationship Mapper (we’ll talk about ORM in next tutorials) and other. A normal projects doesn’t need everything I’ve listed, so “why pay for what you don’t need”?
And I forgot to say, that CodeIgniter and other PHP Frameworks listed here are Open Source, so you can use them for free and modify then as you wish. There is a bunch of libraries, helpers and other open source software built on CodeIgniter.
Another thing I like to CodeIgniter is the way they help me building my links. What can you do when you want to move your application to another domain? Do you use relative paths to your files?
Do you create links with your domain everywhere and then mass-edit? Here CodeIgniter will help you with 2 methods: base_url() and site_url().
Both methods return the main URL for your application, but there’s a small difference between them, you’ll learn about this later. Let’s say you have your script installed on your-domain.com/my_blog, base_url() will return you exactly your URL.
CodeIgniter has also a fantastic form validation library, which helps you validate your data easily, before making database operations.
Sending emails has never been easier. CodeIgniter even has an email class for you. Do you want to send an email? Ok, here is the code:
$this->email->to("[email protected]"); $this->email->from([email protected]'); $this->email->subject('Hello from CodeIgniter'); $this->email->message('This is my first email from CodeIgniter.'); $this->email->send();
Want to send an attachment too? Add this line;
And you’re done.
There are still much more tricks to discover, but this is enough for today.
Now you know what is PHP, what is a PHP Framework, what is CodeIgniter and why did I pick CodeIgniter.
This tutorial was like a simple intro to make you more familiar with PHP frameworks and especially with CodeIgniter, my favorite. Good luck and see you next time.
http://codeigniterdirectory.com/ – A directory where you may find a low of useful stuff regarding CodeIgniter
http://codeigniter.com/wiki – The official CodeIgniter Wiki
For more CodeIgniter tutorials, make sure you subscribe to our RSS feed. If you have any questions about CodeIgniter, feel free to leave a comment!