Theory
Plumbing Rewrite करना बंद कीजिए
इस unit में पहले आपने input validation, session handling, और file uploads HAND-WRITE किए। यह काम किया, पर हर project वही fiddly, security-sensitive code rewrite करता, और हर hand-rolled version एक नए bug का risk लेता है।
यह exactly वह है जो एक framework आपको spare करता है। CodeIgniter repetitive jobs के लिए tested, reusable tools ship करता है: एक validation library, session और flash messaging, safe uploads, और loadable helpers। यह final PHP lesson portal को framework के power tools इस्तेमाल करते दिखाता है इन्हें reinvent करने की बजाय, जो एक framework adopt करने की पूरी वजह है।
Theory
Validation और Flashdata
Form validation library: checks hand-write करने की बजाय, आप RULES declare करते हैं: required, valid_email, min_length[6]: और CodeIgniter इन सबको एक call में check करता है, consistent error messages generate करते हुए। Cleaner, और bespoke if statements से wrong होना ज़्यादा मुश्किल।
Sessions और flashdata: CI PHP sessions को wrap करता है और flashdata add करता है: session data जो exactly ONE अगली request के लिए survive करता है और फिर auto-delete होता है। यह एक one-time message के लिए perfect है: एक successful registration के बाद आप एक confirmation page पर REDIRECT करते हैं और एक flash message set करते हैं "Registration successful!"; यह एक बार दिखता है, फिर vanish हो जाता है, तो एक page refresh इसे repeat नहीं करता। यह standard post-redirect-get pattern है।
Practical
Validation + Redirect के बाद एक Flash Message
<?php
// Inside a controller method handling the registration POST
public function submit() {
$rules = [
"name" => "required|min_length[2]",
"email" => "required|valid_email",
];
if (! $this->validate($rules)) {
// Validation failed: show the form again with errors
return view("register", ["errors" => $this->validator->getErrors()]);
}
// Success: save via the Model, then set a ONE-TIME flash message
session()->setFlashdata("msg", "Registration successful!");
return redirect()->to("/thanks"); // flash shows once on /thanks
}
?>
Theory
Uploads, Helpers, और Libraries
File uploads: CI का upload handling type और size validate करता है और file को safely move करता है: Unit 2 के raw $_FILES और move_uploaded_file काम (और इसके traps) को एक tested tool में wrap करते हुए।
Helpers बनाम Libraries, एक common exam pair:
- Helpers standalone FUNCTIONS का collection हैं (जैसे
form_helper,url_helper) जिन्हें आप ज़रूरत पड़ने पर load करते हैं - Libraries CLASSES हैं जो features provide करती हैं (session, validation, email)
आप जो helper या library चाहिए इसे LOAD करते हैं और इस्तेमाल करते हैं, framework के tested code को reuse करते हुए। पूरा theme: एक framework आपको reliable building blocks देता है ताकि आप हर बार plumbing hand-craft करने की बजाय features assemble करें।
Quiz
CodeIgniter में flashdata किसके लिए इस्तेमाल होता है?
- Database में हमेशा के लिए stored permanent data
- Session data जो exactly ONE अगली request के लिए persist करता है फिर auto-delete होता है: 'Registration successful!' जैसे one-time messages के लिए ideal
- एक faster type की cookie
- एक Flash animation में दिखाया गया data
Show the answer
Session data जो exactly ONE अगली request के लिए persist करता है फिर auto-delete होता है: 'Registration successful!' जैसे one-time messages के लिए ideal
Flashdata session data है जो exactly NEXT request के लिए रहता है और फिर automatically remove हो जाता है, जो इसे एक redirect के बाद दिखाए गए one-time notifications के लिए perfect बनाता है (post-redirect-get pattern): 'Registration successful!' message confirmation page पर एक बार दिखता है और अगर user refresh करे तो reappear नहीं होता। Option A permanent storage (database/Model) describe करता है, flashdata की one-shot nature का opposite। Option C इसे cookies से confuse करता है (flashdata server-side session data है)। Option D एक unrelated technology पर एक pun है। याद रखिए: flashdata = one-request-only session message।
Think first
Redirect, फिर Flash क्यों?
एक successful registration के बाद, controller REDIRECT क्यों करता है और एक FLASH message set करता है, same page पर directly 'Success!' echo करने की बजाय? फिर tap कीजिए।
Show the answer
Post-redirect-get pattern की वजह से, जो double-submission problem solve करता है। अगर आप एक POST process करने के बाद directly 'Success!' echo करें, और user फिर REFRESH करे, browser form को RE-SUBMIT करता है: इन्हें दो बार register करते हुए। एक successful POST के बाद एक confirmation page (एक GET request) पर REDIRECT करके, एक refresh बस harmless confirmation page reload करता है, submission नहीं। Flashdata उस एक redirect के across 'Registration successful!' message carry करता है और फिर vanish हो जाता है, तो यह exactly एक बार दिखता है। तो redirect-then-flash एक clean, refresh-safe confirmation देता है: एक छोटा pattern जो एक real, common bug रोकता है, यही वजह है frameworks flashdata build करते हैं।
Watch out
CI Core-Feature Traps
जो CI Provide करता है उसे Hand-Rolling करना: इन्हें rewrite करने की बजाय validation library, session और upload tools इस्तेमाल कीजिए।
Helpers और Libraries को Confuse करना: helpers function collections हैं; libraries classes हैं।
Flashdata से Persist होने की Expect करना: यह design से सिर्फ़ ONE request चलता है।
बिना Redirect के Success Echo करना: refresh पर double-submission cause करता है; redirect + flash इस्तेमाल कीजिए।
इस्तेमाल से पहले एक Helper Load न करना: पहले helper/library load कीजिए, वरना इसके functions unavailable हैं।
Theory
Units 1-3 Complete: एक Real PHP Application
आपने event portal को hello.php से एक structured, database-backed, framework-organised application तक ले गए: Core PHP, advanced features, injection-safe queries वाला MySQL, AJAX, और इसके power tools वाला CodeIgniter MVC। यही server-side web development है, end to end। Unit 4 kind में अलग है: Indian Knowledge System astronomy topic (Suryasiddhanta और Varahamihira), factually और respectfully studied। Web app done है; IKS unit अपने आप पर खड़ा है।
Summary
Key takeaways
- एक framework tested, reusable tools provide करता है ताकि आप plumbing hand-roll करने की बजाय features assemble करें।
- CI की validation library declarative rules (required, valid_email, min_length) एक call में auto error messages के साथ check करती है।
- Flashdata session data है जो exactly एक अगली request के लिए रहता है फिर auto-delete होता है: एक redirect के बाद one-time messages के लिए ideal।
- Redirect-then-flash (post-redirect-get) refresh पर double submission रोकता है।
- CI का upload handling raw $_FILES काम को wrap करते हुए files safely validate और move करता है।
- Helpers functions के collections हैं; libraries feature classes हैं; जो चाहिए वह load कीजिए।
- Memory hook: framework को plumbing करने दीजिए; flashdata एक बार दिखता है और disappear हो जाता है।