← Back to PHP Course | Chapter 15: Web Development | Lesson 3 of 12

PHP Laravel Introduction

Laravel PHP में websites बनाने के लिए एक बड़ा ready-made toolbox है, pehle se बने tricky pieces वाली एक LEGO kit जैसा। यह आपको एक neat structure देता है ताकि आप तेज़ी से बनाएँ।
Syntax
php
Route::get("/path", [ControllerName::class, "method"]);   // routes/web.php

@foreach ($items as $item)   {{-- Blade template --}}
    {{ $item->property }}
@endforeach

$model = ModelName::find($id);   // Eloquent

Laravel क्या है?

Laravel सबसे widely adopted PHP framework है, MVC pattern के आसपास बना है और expressive syntax और built-in tools के एक rich set के through developer productivity को prioritize करने के लिए जाना जाता है।

उदाहरण: What is Laravel?

php
<?php
// Print "Laravel: MVC-based, developer-friendly PHP framework" to the output
echo "Laravel: MVC-based, developer-friendly PHP framework";
?>

Basic Routing

Laravel का router incoming URLs को एक single, readable routes file में specific controller methods या inline closures में map करता है, आपके application के URL structure को एक नज़र में देखना आसान रखते हुए।

उदाहरण: Basic Routing

php
<?php
// routes/web.php:
// Route::get('/users/{id}', [UserController::class, 'show']);
echo "Maps a URL pattern to a controller method";
?>

Blade Templating

Blade, Laravel का templating engine, familiar-looking template syntax (जैसे @if और @foreach) को plain, cached PHP में compile कर देता है, आपको performance से समझौता किए बिना clean templates देते हुए।

उदाहरण: Blade Templating

php
<?php
// Blade template syntax (compiles to plain PHP):
// @if($isAdmin) <p>Admin</p> @endif
echo "@if and @foreach compile down to cached plain PHP";
?>

Eloquent ORM

Eloquent Laravel का ActiveRecord-style ORM है, आपको database rows को PHP objects (जैसे User::find(1)->save()) की तरह query और manipulate करने देते हुए common operations के लिए raw SQL लिखने के बजाय।

उदाहरण: Eloquent ORM

php
<?php
// $user = User::find(1);
// $user->name = "Alice";
// $user->save();
echo "Query and manipulate rows as PHP objects instead of raw SQL";
?>

Artisan CLI Commands

Artisan Laravel का command-line tool है, कुछ सेकंड में नए controllers, models, और migrations scaffold करने और caches clear करने या test suite चलाने जैसे maintenance tasks चलाने के लिए इस्तेमाल होता है।

उदाहरण: Artisan CLI Commands

php
<?php
// Run in terminal: php artisan make:controller UserController
echo "Artisan scaffolds controllers, models, and migrations";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी project clone करने के बाद composer install चलाना या .env copy करना भूल जाना।
  2. Blade में {{ }} इस्तेमाल करना उस HTML के लिए जिसे escape नहीं होना चाहिए, जबकि {!! !!} raw output print करता है।
  3. POST routes पर web middleware या CSRF token भूल जाना, ताकि forms एक 419 error के साथ fail हों।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.