Laravel Controllers & The Request Lifecycle — Path to Laravel (Article 08)🧠⚡
Meet the brain of your application! Learn how to use Laravel Controllers, handle user data via Request objects, and master the MVC…
Laravel Controllers & The Request Lifecycle — Path to Laravel (Article 08)🧠⚡
Meet the brain of your application! Learn how to use Laravel Controllers, handle user data via Request objects, and master the MVC architecture.
කොහොමද ඔයාලට! 👋
අපේ “Path to Laravel” ලිපි මාලාවේ අද අපි ඉගෙන ගන්නේ Laravel වල තියෙන වැදගත්ම කොටසක් ගැන. ඒ තමයි Controllers.

ඔයා හෝටලයකට ගියාම ඔයා බලන “මෙනු එක” තමයි Route එක. ඔයාට ඕන කෑමක් තෝරාගත්තම, ඒ ඕඩර් එක කුස්සියට අරන් ගිහින්, කෑම ලෑස්ති කරලා ඔයාගේ මේසෙට ගෙනත් දෙන “වේටර්” (Waiter) හරි “ෂෙෆ්” (Chef) හරි තමයි Controller එක. අන්තිමට ඔයාට ලැබෙන රසවත් “කෑම පිඟාන” තමයි View එක.
අද අපි බලමු මේ ක්රියාවලිය (Request Lifecycle) ප්රායෝගිකව සිද්ධ වෙන්නේ කොහොමද කියලා.
MVC Architecture එක සරලව 🏛️
Laravel වැඩ කරන්නේ MVC (Model-View-Controller) කියන architecture එකට.
- Model: ඩේටාබේස් එකත් එක්ක ගනුදෙනු කරන කොටස.
- View: User ට පේන HTML/Blade කොටස.
- Controller: මේ දෙක මැද ඉඳන් තීරණ ගන්නා මොළය.

Understanding the core flow: How Model, View, and Controller work together in modern web architecture
Controller එකක් හදමු 🏗️
Laravel වල හැමදේකටම වගේ Controller එකක් හදන්නත් අපිට Artisan ගේ උදව් ගන්න පුළුවන්. Terminal එකේ මේ command එක ගහන්න:
php artisan make:controller UserController
දැන් ඔයාගේ ප්රොජෙක්ට් එකේ app/Http/Controllers ෆෝල්ඩර් එක ඇතුළේ UserController.php කියලා අලුත් ෆයිල් එකක් හැදිලා තියෙයි. ඒක ඇතුළේ අපිට පුළුවන් අපේ Functions ලියන්න.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function showProfile()
{
$name = "Dinushka";
return view('profile', compact('name'));
}
}
Route එකකින් Controller එකට වැඩ පවරමු 🔗
දැන් අපි route එකට කියන්න ඕනේ යම් URL එකකට කවුරුහරි ආවොත් මේ Controller එකේ තියෙන අහවල් Function එක රන් කරන්න කියලා. මේක කරන්නේ routes/web.php එකේ මෙහෙමයි:
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
// URL එක /profile නම්, UserController එකේ showProfile function එකට යන්න
Route::get('/profile', [UserController::class, 'showProfile']);
Request Lifecycle: User එවන data ලබා ගැනීම 📥
වෙබ් ඇප් එකකදී User කෙනෙක් ෆෝම් එකක් හරහා data එවනවා (උදා: Name, Email). මේ data Laravel ඇතුළට එන්නේ **Request කියන Object එක හරහායි. මේක තමයි Laravel වල Request Lifecycle** එකේ තීරණාත්මකම තැන.
Controller එකේදී අපිට මේ දත්ත මෙහෙම ලබාගන්න පුළුවන්:
public function store(Request $request)
{
// User එවපු නම ලබාගැනීම
$username = $request->input('name');
// එහෙමත් නැත්නම් මෙහෙමත් පුලුවන්
$email = $request->email;
return "User " . $username . " has been registered with " . $email;
}
Resource Controllers 🛠️
ඔයාට ඕනේ නම් එකම Controller එකක් ඇතුළේ CRUD (Create, Read, Update, Delete) වැඩ ඔක්කොම කරන්න, Laravel වලට පුළුවන් ඒකට අවශ්ය කරන හැම Function එකක්ම (index, create, store, show, edit, update, destroy) එකපාර හදලා දෙන්න.
ඒකට පාවිච්චි කරන්නේ මේ කමාන්ඩ් එක:
php artisan make:controller PostController --resource
මේකෙන් ඔයාට ලැබෙන වාසිය තමයි route එකේ එක පේළියකින් මේ ඔක්කොම රවුට් ලියාගන්න පුළුවන් වීම:
Route::resource('posts', PostController::class);
Request Lifecycle එක වැඩ කරන්නේ කොහොමද? 🔄
අපි Controller එකක් ඇතුළේ වැඩ කරද්දී, ඇත්තටම වෙබ් අඩවිය ඇතුළේ සිද්ධ වෙන ක්රියාවලිය (Lifecycle) මෙහෙමයි:
- Request: User URL එකක් click කරයි (උදා:
/profile). - Routing:
web.phpමඟින් ඒ URL එක අදාළ Controller එකට යොමු කරයි. - Middleware: (අපි ඉස්සරහට ඉගෙන ගන්නවා) route එකේ ඉඳන් Controller එකට යන පාරේ ඉන්න මුරකරුවෙක් වගේ data පරීක්ෂා කරයි.
- Controller: Controller එක විසින් data සකසා (Logic) අවශ්ය නම් Model එක හරහා ඩේටාබේස් එකට කතා කරයි.
- Response: අවසානයේ Controller එක විසින් View එකක් හෝ data ටිකක් නැවත user ට (Browser එකට) එවයි.

Visualizing the Laravel Request Lifecycle: from user request through routing, middleware, and controller to the final response.
Expert Tips for Better Controllers 🚀
Single Action Controllers (__invoke) 🎯
සමහර වෙලාවට අපිට ඕන වෙන්නේ එක Controller එකකින් එකම එක වැඩක් විතරක් කරන්න (උදා: Report එකක් generate කිරීම). එතකොට අපිට index, show වගේ methods ගොඩක් ඕනේ නැහැ. අපිට පුළුවන් __invoke කියන method එක පාවිච්චි කරන්න.
- මේකෙ විශේෂත්වය තමයි route එකේදී method එකේ නම ලියන්න ඕනේ නැහැ.
Route::get('/report', ReportController::class);විදිහට කෙලින්ම ලියන්න පුළුවන්.
Dependency Injection 💉
Laravel වල තියෙන සුපිරිම වැඩක් තමයි මේක. අපිට අවශ්ය වෙනත් Classes (උදා: Request, Mailer) Controller එකේ method එක ඇතුළේදී “Argument” එකක් විදිහට දුන්නම, Laravel විසින්ම ඒක අපිට ලෑස්ති කරලා දෙනවා. අපි උඩදී Request $request කියලා පාවිච්චි කළෙත් මේ ක්රමයම තමයි.
“Skinny Controllers” 📉
මතක තියාගන්න, Controller එකක් ඇතුළේ ලොකු Logic ලියන්න එපා. Controller එකේ වැඩේ වෙන්න ඕනේ “Request එක අරගෙන අදාළ තැනට යොමු කරන එක” විතරයි. ලොකු Calculations සහ Database වැඩ Models වලට හෝ Services වලට පවරන්න (මේකට අපි “Skinny Controller, Fat Model” කියලා කියනවා).

Exploring advanced Laravel controller techniques: from single-action invoke methods and dependency injection to building streamlined ‘skinny’ controllers that delegate logic.
🎯 Conclusion & What’s Next?
අද අපි ඉගෙන ගත්තා Controller එකක් කියන්නේ මොකක්ද, ඒක හදන්නේ කොහොමද සහ route එකත් එක්ක ඒක සම්බන්ධ කරන්නේ කොහොමද කියලා. දැන් ඔයා දන්නවා Laravel ඇප් එකක Logic එක clean විදිහට තියාගන්නේ කොහොමද කියලා.
ඊළඟ ලිපියෙන් අපි කතා කරමු Laravel වල හදවත වගේ තියෙන Database & Migrations ගැන. එතනදී තමයි අපි ඇත්තටම data save කරන්න ඉගෙන ගන්නේ! 🗄️
ඔයා Controller එකක් හදද්දී මොකක් හරි ගැටලුවක් ආවද? route එක ලියාගන්න බැරි වුණාද? පහළින් Comment කරන්න! 👇
✍️ Written by Dinushka Tharidu
PHP #Laravel #MVC #Controllers #BackendDevelopment #SoftwareEngineering #PathToLaravel #CodingLife #SriLankaTech #DTech #ExpertGuide #WebDevelopment
메타데이터
- post_id
- abbbb766f74f
- slug
- laravel-controllers-the-request-lifecycle-path-to-laravel-article-08-abbbb766f74f
- url
- https://medium.com/@dinushkatharidu/laravel-controllers-the-request-lifecycle-path-to-laravel-article-08-abbbb766f74f
- canonical_url
- https://medium.com/@dinushkatharidu/laravel-controllers-the-request-lifecycle-path-to-laravel-article-08-abbbb766f74f
- author_url
- https://medium.com/@dinushkatharidu
- status
- ok
- fetched_at
- 2026-06-12 07:40:50