Laravel MVC Architecture and Implementation Notes
# Laravel MVC Architecture and Implementation ## MVC Architecture in Laravel The MVC architecture in Laravel consists of three main components: * **Model**: Handles data and business logic. * **View**: Displays data to the user. * **Controller**: Manages input and coordinates between Model and View. ## Role of MVC Components in Laravel * **Model**: Interacts with the database using Eloquent ORM. * **View**: Uses the Blade templating engine to render HTML. * **Controller**: Receives input from routes, retrieves data from Models, and passes data to Views. ## Benefits of MVC in Laravel * **Separation of Concerns**: Leads to cleaner code organization. * **Maintainability**: Makes it easier to update or debug specific components. * **Scalability**: Allows teams to work on different parts simultaneously. ## Blog Post Example - MVC Implementation To display a list of blog posts, the following components are used: * **Model**: `Post` model. * **Controller**: `PostController`. * **View**: `index.blade.php`. * **Route**: `Route::get('/posts', [PostController::class, 'index']);` ## Post Model Code ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { // ... } ``` ## PostController Code ```php public function index() { $posts = Post::all(); return view('posts.index', compact('posts')); } ``` ## Blade View File - index.blade.php ```blade @foreach ($posts as $post)
{{ $post->title }}
{{ $post->body }}
@endforeach ``` ## MVC Advantages in Large Projects * Teams can focus on specific layers. * Encourages reusable components. * Simplifies debugging and testing. ## Core PHP User Management System A basic user management system includes: * Database Connection using `mysqli`. * Insert Form: Name, Email. * Display Users in a table format. ## PHP Code - Database Connection ```php $conn = new mysqli("localhost", "root", "", "testdb"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ``` ## Insert & Display Users * Form Input: name, email. * Insert Code (SQL INSERT). * Fetch and display all users (SQL SELECT). ## Security Considerations * Input validation using `filter_var()`. * Use prepared statements to prevent SQL Injection. * Sanitize output to prevent XSS. ## Laravel CMS - CRUD for Blog Posts * Use `php artisan make:controller PostController --resource` to create a resource controller. * `Route::resource('posts', PostController::class);` to define CRUD routes. ## CRUD Blade Views * `create.blade.php`, `edit.blade.php`, `index.blade.php`, `show.blade.php` are used for CRUD operations. * Form validation using `request()->validate()`. ## Middleware in Laravel * **What is Middleware?** Filter for HTTP requests. * Executes before reaching the controller. * Built-in Example: `auth`, `verified`. ## Applying Middleware * Single Route: `->middleware('auth')` * Group: `Route::middleware('auth')->group(...)` * Controller: `$this->middleware('auth');` ## Custom Middleware & Admin Use Case * `php artisan make:middleware IsAdmin` to create custom middleware. * Check `auth()->user()->is_admin` to verify admin status. * Register in `Kernel.php`. * Apply with `->middleware('is_admin')`. ## Middleware Benefits * Clean code * Centralized access logic * Reusable across routes/controllers