Codeigniter is one of the popular PHP frameworks which is mandatory for every web developer who wants to work somewhere in a software house or wants to work professionally in the field of web development.
CodeIgniter is a popular PHP framework known for its simplicity, flexibility, and rapid development capabilities. Whether you are a seasoned developer or a beginner, CodeIgniter offers an excellent platform to build powerful PHP applications. In this blog post, we will walk you through the process of installing CodeIgniter, running it in XAMPP, and building a basic PHP app using this framework.
What’s a Framework?
A framework is a set up of functionality which is available to the developer for leveraging his/her web development tasks, A framework makes the web developer’s job easier by giving him ready made functions and methods to use in his/her projects. It also reduces the work flow and makes the application faster and safer. There are many frameworks available in the market, and most of them are free to use with open source approach.
What’s CodeIgniter?
PHP is a popular server side scripting language which has many frameworks such as Laravel, Codeigniter, PHPCake, Zend and Magento etc. Codeigniter is the easiest and light weight framework which is the need of every PHP developer today. It’s free to use and open source (Open source means everyone can contribute to this framework and can also see the source code).
What’s MVC?
MVC stands for Model View Controller, this is a design pattern that is now very popular, and therefore, every PHP framework has adopted this approach. So most of the time, you’ll find a Framework working on this pattern (MVC), and you can also create an MVC with your own logic’s and PHP skills.
Furthermore, in the MVC design pattern, the Model is for creating back-end functionality such as the database, queries and other logic’s related to database-driven tasks. The View is for creating the front-end work such as HTML pages and CSS libraries. And finally, the Controller is for action, it works like connecting the Model with the View.
How to Install CodeIgniter
Before diving into development, let’s start by installing CodeIgniter on your local machine. Follow these steps to get started:
Step 1: Download CodeIgniter
Visit the official CodeIgniter website (https://codeigniter.com/) and download the latest version of the framework.
Step 2: Extract the Archive
Once the download is complete, extract the downloaded archive to a location of your choice on your computer.
Step 3: Configure CodeIgniter
After extracting the files, navigate to the “application/config” folder and open the “config.php” file. Here, you can configure essential settings such as your base URL and database connection.
Step 4: Set Encryption Key
In the same “config.php” file, you will find an option to set an encryption key. It’s crucial for security purposes, and CodeIgniter will generate it for you automatically. Keep this key secure and don’t share it with others.
Step 5: Test the Installation
To ensure that CodeIgniter is correctly installed, open your web browser and enter the URL to your CodeIgniter directory. If everything is set up correctly, you should see the default welcome page of CodeIgniter.
Run CodeIgniter in XAMPP
To run CodeIgniter using XAMPP, you’ll need to have XAMPP installed on your machine. If you don’t have it already, download and install XAMPP from the official website (https://www.apachefriends.org/index.html). Once you have XAMPP up and running, follow these steps:
Step 1: Move CodeIgniter Files
Copy the extracted CodeIgniter files to the “htdocs” folder within your XAMPP installation directory. This folder is typically located in “C:\xampp\htdocs” on Windows or “/Applications/XAMPP/htdocs” on macOS.
Step 2: Start XAMPP Services
Launch XAMPP and start the Apache and MySQL services. These are essential for running the PHP applications.
Step 3: Create a Database
Open your web browser and go to “http://localhost/phpmyadmin/” to access the phpMyAdmin interface. Create a new database that you’ll use for your CodeIgniter application.
Step 4: Configure Database Connection
In CodeIgniter, navigate to “application/config/database.php” and update the database settings with the appropriate credentials (database name, username, password, etc.).
Step 5: Run CodeIgniter Application
Now that everything is set up, open your web browser and go to “http://localhost/your-codeigniter-directory” to run your CodeIgniter application on XAMPP.
Build PHP App in CodeIgniter
With CodeIgniter installed and running, let’s create a basic PHP application using the framework. We’ll build a simple “To-Do List” application to demonstrate the process:
Step 1: Set Up the Controller
In CodeIgniter, controllers handle the application’s logic. Create a new file named “Todo.php” in the “application/controllers” directory. Define a class inside this file and create functions for adding, deleting, and displaying tasks.
Step 2: Design the Views
Create the views for your application in the “application/views” directory. For the “To-Do List” app, you’ll need views for displaying the tasks and a form to add new tasks.
Step 3: Implement the Model
The model interacts with the database. Create a model file named “Todo_model.php” in the “application/models” directory. Implement functions for adding tasks to the database and fetching tasks from it.
Step 4: Connect the Dots
In the controller, load the model and appropriate views. Implement the necessary logic to handle user interactions and data processing. Use the CodeIgniter documentation for guidance on how to interact with models and views effectively.
Step 5: Test the App
Run your CodeIgniter application in the browser and test the “To-Do List” functionality. Make sure you can add tasks, delete them, and see the list updating accordingly.
Congratulations! You have successfully installed CodeIgniter, set it up with XAMPP, and built a basic PHP application using the framework. From here, you can explore the extensive features CodeIgniter offers, including libraries, helpers, and more, to develop more complex and feature-rich applications. Happy coding!
In this tutorial, we see What is Codeigniter, how to download and install Codeigniter,and how to write your first program using codeigniter.
htaccess code for CodeIgniter——–
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]