1. Home
  2. /
  3. Codeigniter
  4. /
  5. Simple Counter App

Simple Counter App - Codeigniter Typing CST Test

Loading…

Simple Counter App — Codeigniter Code

Demonstrates a simple CodeIgniter controller and routes for a counter using session for state persistence.

<?php

// application/controllers/Counter.php
class Counter extends CI_Controller {
	public function __construct() {
		parent::__construct();
		$this->load->library('session');
		if (!$this->session->has_userdata('count')) {
		$this->session->set_userdata('count', 0);
		}
	}

	public function index() {
		$count = $this->session->userdata('count');
		echo "<h2>Counter: $count</h2>";
		echo "<form method='post' action='/counter/increment'><button type='submit'>+</button></form>";
		echo "<form method='post' action='/counter/decrement'><button type='submit'>-</button></form>";
		echo "<form method='post' action='/counter/reset'><button type='submit'>Reset</button></form>";
	}

	public function increment() {
		$count = $this->session->userdata('count') + 1;
		$this->session->set_userdata('count', $count);
		redirect('/counter');
	}

	public function decrement() {
		$count = $this->session->userdata('count') - 1;
		$this->session->set_userdata('count', $count);
		redirect('/counter');
	}

	public function reset() {
		$this->session->set_userdata('count', 0);
		redirect('/counter');
	}
}

Codeigniter Language Guide

CodeIgniter is a powerful PHP framework with a small footprint, designed for developers who need a simple and elegant toolkit to build full-featured web applications quickly.

Primary Use Cases

  • ▸Developing custom PHP web applications
  • ▸Creating RESTful APIs quickly
  • ▸Rapid prototyping for startups
  • ▸Building internal enterprise tools and dashboards
  • ▸Integrating with third-party APIs or legacy systems

Notable Features

  • ▸Lightweight and fast performance
  • ▸MVC architecture for organized code
  • ▸Built-in security and XSS/CSRF protection
  • ▸Comprehensive libraries and helpers
  • ▸Flexible URL routing and easy configuration

Origin & Creator

Created by EllisLab in 2006, CodeIgniter was designed to offer a simple and lightweight alternative to larger PHP frameworks while maintaining flexibility and speed.

Industrial Note

CodeIgniter is particularly useful for rapid prototyping, small to medium enterprise web applications, and performance-sensitive PHP projects where simplicity and speed are priorities.

Quick Explain

  • ▸CodeIgniter follows the MVC (Model-View-Controller) design pattern, separating application logic, presentation, and data management.
  • ▸It is lightweight and optimized for performance, making it suitable for shared hosting environments.
  • ▸Provides built-in libraries for database abstraction, session management, form validation, and security.
  • ▸Minimal configuration is required to get started, allowing developers to focus on building features rather than setup.
  • ▸CodeIgniter supports RESTful API development, CRUD operations, and integration with third-party libraries.

Core Features

  • ▸MVC framework - clean separation of concerns
  • ▸Database abstraction layer - simplifies CRUD operations
  • ▸Form validation and security helpers
  • ▸Session and cookie management
  • ▸RESTful API support and routing flexibility

Learning Path

  • ▸Learn PHP and MVC architecture
  • ▸Understand CodeIgniter routing and controllers
  • ▸Work with models, views, and database interactions
  • ▸Implement security and validation features
  • ▸Build full applications and RESTful APIs

Practical Examples

  • ▸Building a blog platform with database-backed posts
  • ▸Creating an API for mobile applications
  • ▸Developing an internal CRM dashboard
  • ▸Implementing user authentication and role-based access
  • ▸Integrating payment gateways or third-party APIs

Comparisons

  • ▸CodeIgniter vs Laravel: Lightweight vs full-featured
  • ▸CodeIgniter vs Symfony: Simple setup vs enterprise-grade
  • ▸CodeIgniter vs CakePHP: Minimal vs convention-heavy
  • ▸CodeIgniter vs Slim: Full MVC vs micro-framework
  • ▸CodeIgniter vs Yii: Quick prototyping vs advanced features

Strengths

  • ▸Extremely lightweight and easy to deploy
  • ▸Simple learning curve for beginners
  • ▸Highly flexible and extendable
  • ▸Well-documented and mature framework
  • ▸Suitable for both small and medium projects

Limitations

  • ▸Not as feature-rich as Laravel or Symfony
  • ▸Smaller community compared to newer PHP frameworks
  • ▸Limited built-in ORM capabilities
  • ▸Less modern tooling for advanced PHP practices
  • ▸Not ideal for large-scale enterprise applications with complex architecture

When NOT to Use

  • ▸Very large, enterprise-grade applications needing advanced ORM
  • ▸Projects requiring heavy dependency injection or modern PHP features
  • ▸Applications needing active long-term support for cutting-edge PHP
  • ▸Systems that rely heavily on event-driven architectures
  • ▸Teams preferring fully-featured, opinionated frameworks like Laravel

Cheat Sheet

  • ▸$this->load->model('Model_name');
  • ▸$this->load->helper('url');
  • ▸$this->load->library('session');
  • ▸$this->db->get('table_name');
  • ▸$this->load->view('view_name', $data);

FAQ

  • ▸Is CodeIgniter suitable for large projects? -> Usually better for small to medium apps.
  • ▸Does CodeIgniter use Composer? -> Yes, CI4 supports Composer packages.
  • ▸Is CodeIgniter secure by default? -> It provides basic XSS/CSRF protection, more needed for production.
  • ▸Can I build APIs with CodeIgniter? -> Yes, RESTful APIs are fully supported.
  • ▸Which version is recommended? -> CodeIgniter 4 for modern PHP applications.

30-Day Skill Plan

  • ▸Week 1: Basic controllers, views, and routing
  • ▸Week 2: Database models and CRUD operations
  • ▸Week 3: Form validation, sessions, and security
  • ▸Week 4: RESTful API development and authentication
  • ▸Week 5: Testing, caching, and performance optimization

Final Summary

  • ▸CodeIgniter is a lightweight, fast PHP framework ideal for rapid development.
  • ▸It follows MVC architecture for organized, maintainable code.
  • ▸Supports RESTful APIs, database abstraction, and security features.
  • ▸Well-suited for small to medium applications and rapid prototyping.
  • ▸Despite being lightweight, it is extendable and integrates well with libraries and third-party tools.

Project Structure

  • ▸application/ - contains MVC components, config, and libraries
  • ▸system/ - core framework files
  • ▸public/ - publicly accessible assets
  • ▸writable/ - cache, logs, and session storage
  • ▸tests/ - unit and integration tests

Monetization

  • ▸Build client web applications
  • ▸Offer SaaS solutions using CI
  • ▸Develop APIs for mobile apps
  • ▸Consulting services for rapid prototyping
  • ▸CodeIgniter-based e-commerce solutions

Productivity Tips

  • ▸Organize controllers, models, and views in modules
  • ▸Use Composer for dependency management
  • ▸Leverage built-in libraries and helpers
  • ▸Automate repetitive tasks with CLI tools
  • ▸Document reusable components for team collaboration

Basic Concepts

  • ▸Controller - handles user requests and application logic
  • ▸Model - interacts with the database and represents data
  • ▸View - renders HTML or JSON output to the user
  • ▸Helpers - utility functions for common tasks
  • ▸Libraries - reusable classes providing additional features

Official Docs

  • ▸CodeIgniter 4 User Guide
  • ▸CodeIgniter 4 API Reference
  • ▸CodeIgniter Forum Documentation
  • ▸CI4 Migration Guide

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher