When we are starting a new project, we usually look for the best tools and practices which help us to get the best performance and results. When it comes to web development, we normally chose to use a framework that better meet our needs, depending on the project’s scope. In these cases, it is important to consider aspects like cross-platform support (for development) and native functions to common tasks like database access, cryptography, session control, etc.
For PHP developers, there are a lot of options that can be used for many types of applications, from the most simple to that complex ones, like portals and e-commerces. In this article we are going to see how to start using one of the most popular frameworks today: CodeIgniter.
CodeIgniter is an open source PHP framework that implements the MVC (Model-View-Controller) pattern and can be used to develop many kinds of web applications.
Its simple and clear structure allows us to create a new project with almost zero configuration, saving development time and focusing on the most important parts of our application.
The framework also counts on a thorough and clear documentation, as well as a great community suport on its official forum.
Creating our first project
To create a new project with CodeIgniter, we just need to download the latest stable version in its official site and extract the ZIP file in our server. For instance, if we are using XAMPP, we can extract the compressed file to C:\xampp\htdocs.
It will give us a folder containing three important folders and one file:
- application: this folder contains the project structure in which we will work. Here we can find, for example, the models, views and controllers directories;
- system: in this folder we can find the core of the framework. All the native classes we will need to develop our project are stored in this directory, that should not be moved or modified (except if you are really interested in changing the framework’s core);
- user_guide: here we can see the framework’s documentation. This folder is very useful when we need to consult the manuals, but can’t access the online version. Nevertheless, we don’t need to send this folder to the production environment;
- index.php: this is the central file of our application. Here we can find some important settings like the system and application folder’s path, as well as the application environment (development, production or test).
Into the application/controller folder we can find a default controller (Welcome.php) and a basic view (welcome_message.php) in the views directory. The Welcome controller, by default, will call the “welcome message” when we access it in browser.
Executing the project
If we are using Apache (installed by XAMPP or WAMPP, for instance), we just need to access its base URL, followed by the name of our project’s folder. For example, if we extracted the CodeIgniter downloaded file and renamed the folder to “MyFirstProject”, we can access the address http://localhost:8080/MyFirstProject and we will see a default page, as shown in Figure 1.
Figure 1. Project’s initial page
If you prefer, you can use the PHP embed server to start the application. To do this, open the command prompt, navigate to the project folder and run the following command, so the application will be available at http://localhost:8080:
Now you can open the application folder in your favorite editor, add a new controller and start playing with CodeIgniter.