π 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
π― 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
π 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!"; });
π― 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
π 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!"; } }
Then, update your app/Config/Routes.php
file to use this controller:
$routes->get('/greet', 'MyController::greet');
π― 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>
Modify the controller to return this view:
namespace App\Controllers; class MyController extends BaseController { public function home() { return view('welcome_message'); } }
Update app/Config/Routes.php
to point to this function:
$routes->get('/', 'MyController::home');
π 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! π