CodeIgniter introduction (CI4): installing and configuring; MVC architecture in CodeIgniter; creating models, views, and controllers; URL routing and default controller setup

CodeIgniter MVC पर built एक PHP framework है: Model data handle करता है, View pages दिखाता है, Controller logic चलाता है, और routing एक URL को एक controller method से map करता है: structure जो loose scripts replace करती है।

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Portal Scripts का एक Pile है

Event portal काम करता है, पर इसे honestly देखिए: login.php, search.php, register.php, हर एक database code, HTML, और logic का एक साथ tangled mix। 20 और pages add कीजिए और यह unmaintainable बन जाता है: header बदलिए और आप 30 files edit करते हैं; एक bug ढूँढिए और आप spaghetti के through hunt करते हैं।

Professional teams इस तरह build नहीं करतीं। ये एक framework इस्तेमाल करती हैं जो STRUCTURE enforce करता है। PHP का classic teaching framework CodeIgniter है, और इसका organising idea MVC है: एक clean separation जो यह lesson portal को properly rebuild करने के लिए इस्तेमाल करता है।

Theory

MVC: तीन Jobs, Cleanly Separated

MVC (Model-View-Controller) एक application को 3 layers में split करता है, हर एक का ONE job है:

  • Model: DATA layer: database से बात करता है, registrations hold करता है
  • View: PRESENTATION layer: वह HTML जो user देखता है
  • Controller: LOGIC layer: request receive करता है, Model से data माँगता है, और इसे View को हाथ में देता है

Separation of concerns payoff है: designer database code को touch किए बिना Views edit करता है; आप pages तोड़े बिना Model में database बदलते हैं; logic Controllers में रहता है, HTML के through scattered नहीं। Tangled script 3 tidy, independent layers बन जाती है।

Theory

Setup और Routing

Setup (CI4): Composer (या download) से install कीजिए, config files में base URL और database credentials configure कीजिए, और built-in development server चलाइए। CodeIgniter plumbing provide करता है: routing, database access, validation, sessions, तो आप features लिखते हैं, infrastructure नहीं।

Routing एक URL को एक Controller METHOD से map करता है। /events की एक request (मान लीजिए) Events controller के index() method को routed होती है; default controller home route handle करता है; routes Config/Routes.php में declared होते हैं। तो URL अब एक physical file (events.php) की तरफ़ point नहीं करता; यह एक controller action की तरफ़ point करता है, और framework इसे wire up करता है। यही indirection है जो MVC को organised रखता है।

Practical

एक Controller जो एक View Load करता है (CI4 Shape)

<?php
// app/Controllers/Events.php
namespace App\Controllers;

class Events extends BaseController {
  public function index() {
    // CONTROLLER: coordinate. Ask a Model for data (simplified here):
    $data["events"] = ["Garba Night", "Coding Contest"];

    // Hand the data to a VIEW to render:
    return view("events_list", $data);
  }
}
// A request routed to /events runs index(), which renders the view.
// The VIEW (app/Views/events_list.php) just displays $events as HTML.
// A MODEL (app/Models/EventModel.php) would fetch $events from MySQL.
?>

Quiz

MVC में, database से बात करने और data manage करने के लिए कौन सी layer responsible है?

  1. View
  2. Model
  3. Controller
  4. Router
Show the answer

Model

MODEL data layer है: यह database interaction और business data handle करता है (registrations fetch और save करना)। VIEW सिर्फ़ presentation है (वह HTML जो user देखता है)। CONTROLLER coordinate करता है: यह request receive करता है, Model से data माँगता है, और इसे View को pass करता है, पर clean MVC में इसमें खुद raw database code नहीं होना चाहिए। Router (3 MVC layers में से एक नहीं) URLs को controller methods से map करता है। इन jobs को separate रखना ही पूरा point है: data Model में, display View में, coordination Controller में। इन्हें mix करना (एक View के अंदर SQL) वह anti-pattern है जिसे रोकने के लिए MVC exist करता है।

Think first

MVC के Through एक Request Trace कीजिए

एक user CodeIgniter portal पर /events visit करता है। Routing और 3 MVC layers के through request को rendered page तक trace कीजिए। फिर tap कीजिए।

Show the answer

1: Routing URL /events को Events controller के index() method से match करता है। 2: Controller (Events::index) चलता है: यह coordinator है। 3: यह Model (EventModel) से event data माँगता है, जो MySQL query करता है और records return करता है: data logic यहाँ रहती है। 4: controller वह data एक View (events_list.php) को pass करता है, जो इसे HTML की तरह render करता है: presentation logic यहाँ रहती है। 5: finished HTML browser को वापस जाता है। हर layer ने exactly एक job किया: controller को route, controller coordinate करता है, model fetch करता है, view display करती है। वह clean flow, URL से route से controller से model से view, MVC request cycle है, और इसे recite करना core exam answer है।

Watch out

MVC / CodeIgniter Traps

View या Controller में SQL: database code Model में belong करता है; इसे कहीं और leak करना separation तोड़ता है।

Controller में HTML: presentation View में belong करता है; controllers coordinate करते हैं, ये pages echo नहीं करते।

URL को एक File मानना: CI में, routes URLs को controller METHODS से map करते हैं, physical .php files से नहीं।

Config Skip करना: base URL और database settings configure होने चाहिए वरना app misbehave करता है।

Framework से Fighting करना: इनके around hand-rolling करने की बजाय CI के provided tools (routing, validation) इस्तेमाल कीजिए।

Theory

Structure In Place; अब इसके Power Tools

MVC और routing के साथ, portal professional तरीके से organised है: data, presentation और logic cleanly separated। Final Unit 3 lesson CodeIgniter के built-in POWER TOOLS इस्तेमाल करता है: इसकी form-validation library, session और flashdata handling, file uploads, और reusable helpers और libraries, ताकि framework वह repetitive काम करे जो आपने unit में पहले hand-coded किया था। फिर Unit 4 IKS astronomy topic की तरफ़ जाता है। Framework अगला अपना keep earn करता है।

Summary

Key takeaways

  • एक framework structure और reusable components provide करता है; CodeIgniter एक classic PHP MVC framework (CI4) है।
  • MVC concerns separate करता है: Model (data + database), View (presentation/HTML), Controller (logic + coordination)।
  • Separation of concerns apps को maintainable बनाता है: दूसरे layers तोड़े बिना एक layer बदलिए।
  • Setup: Composer से install कीजिए, base URL और database configure कीजिए, dev server चलाइए।
  • Routing एक URL को एक Controller method से map करता है (एक physical file नहीं); default controller home route handle करता है।
  • Request cycle: URL -> route -> controller -> model (data) -> view (HTML) -> browser।
  • Memory hook: Model data, View display, Controller coordinates।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Database Interaction and CodeIgniter Framework

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

CodeIgniter introduction (CI4): installing and configuring; MVC architecture in CodeIgniter; creating models, views, and controllers; URL routing and default controller setup · Web Framework and Services (Major-12) · Gri-Learn