PHP CodeIgniter Basics

πŸš€ PHP CodeIgniter Basics – Getting Started with CodeIgniter

CodeIgniter is a lightweight yet powerful PHP framework for building web applications quickly and efficiently. It follows the Model-View-Controller (MVC) architecture and is known for its simplicity and speed. Let’s dive into CodeIgniter! 🌟


🌍 What is CodeIgniter?

CodeIgniter is an open-source PHP framework that simplifies development by providing built-in libraries for common tasks like database handling, session management, and form validation.

  • Lightweight & Fast: Requires minimal configuration and loads quickly.
  • Easy to Learn: Perfect for beginners and advanced users alike.
  • MVC Architecture: Organizes code into models, views, and controllers.
  • Secure: Built-in protection against XSS, CSRF, and SQL injection.

πŸ“Œ Step 1: Install CodeIgniter

βœ… Installing CodeIgniter via Composer

The easiest way to install CodeIgniter is through Composer. Run the following command:

composer create-project codeigniter4/appstarter myApp

Try It Now

🎯 This will: Download CodeIgniter 4 and set up a new project folder myApp.


πŸ“Œ Step 2: Start the CodeIgniter Development Server

Once installed, navigate to your project folder and start the server:

cd myApp
php spark serve

Try It Now

πŸ” Output: Your CodeIgniter app will be available at http://localhost:8080.


πŸ“Œ Step 3: CodeIgniter Routing (Creating a Simple Route)

Routes in CodeIgniter define how your application responds to HTTP requests. Open the app/Config/Routes.php file and add a new route:

$routes->get('/hello', function () {
    return "Hello, CodeIgniter!";
});

Try It Now

🎯 Now, visit: http://localhost:8080/hello to see the message.


πŸ“Œ Step 4: Creating a Controller

CodeIgniter uses controllers to handle requests. Create a new controller using:

php spark make:controller MyController

Try It Now

πŸ“ This creates: app/Controllers/MyController.php.

Now, open the controller and add a function:

namespace App\Controllers;

class MyController extends BaseController
{
    public function greet() {
        return "Hello from Controller!";
    }
}

Try It Now

Then, update your app/Config/Routes.php file to use this controller:

$routes->get('/greet', 'MyController::greet');

Try It Now

🎯 Now, visit: http://localhost:8080/greet to see the output.


πŸ“Œ Step 5: Creating a View

CodeIgniter uses views for rendering HTML. Let’s create a new view:

<!-- app/Views/welcome_message.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to CodeIgniter</title>
</head>
<body>
    <h1>Hello, CodeIgniter World!</h1>
</body>
</html>

Try It Now

Modify the controller to return this view:

namespace App\Controllers;

class MyController extends BaseController
{
    public function home() {
        return view('welcome_message');
    }
}

Try It Now

Update app/Config/Routes.php to point to this function:

$routes->get('/', 'MyController::home');

Try It Now

πŸ” Output: This renders the welcome page in CodeIgniter.


🎯 Summary

  • βœ… CodeIgniter is a lightweight PHP framework following the MVC pattern.
  • βœ… Routes are defined in app/Config/Routes.php.
  • βœ… Controllers handle logic and respond to requests.
  • βœ… Views make HTML rendering easy and efficient.

πŸš€ Next Steps

Now that you’ve learned the basics, try working with CodeIgniter Models & Databases to build dynamic applications! πŸš€