Laravel is an open-source PHP framework that adheres to the model-view-controller design pattern. Developers are spared the need to build from scratch and can alleviate security concerns. Laravel recycles components from other frameworks when creating web applications, including features like authentication and authorization, template engines, validation, form handling, and more.
MVC architecture
MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separating between the software's business logic and display.
Model: Manages data and business logic
View: Handles layout and display
Controller: Routes commands to the model and view parts
(Cited: MDN)
Searching the images of "MVC design pattern Laravel" will definitely help us understand those relationships.
In Laravel, those are handled in the directories of app\Models, resources\views, and app\Http\Controllers.
app\Models
Laravel comes packaged with Eloquent, which is worth delving into later. It handles so much that developers only need to create a database table in the database\migrations directory along with the corresponding model. This eliminates the need to write SQL queries.
Firstly, we create a database in the database\migrations directory. We utilize the Schema to define tables, columns, and indexes. Next, we create a corresponding model that interacts with the table. Here, we can define relationships such as hasMany and belongsTo.
database\migrations\20xx_01_23_456789_create_flights_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('flights', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('flights');
}
};
app\Models\Flight.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
// ...
}
(Cited: Laravel)
Eloquent is smart enough to guess which model is for which table.
The model name starts with a capital letter, in this case, "Flight", and the table name should be "snake_case" and plurals, that is, "flights".
resources\views
To render view pages, we have the flexibility to choose between using PHP or JavaScript frameworks like Vue and React. Since my focus has been primarily on PHP within the Laravel environment, I will discuss PHP-related options here. Laravel offers a powerful template engine known as Blade, which introduces a syntax for seamlessly embedding PHP data into HTML. Additionally, there's another innovative framework called Livewire that enhances interactivity and modernizes the development experience.
Blade
<div>
@foreach ($users as $user)
Hello, {{ $user->name }} <br />
@endforeach
</div>
Livewire
<div>
<button wire:click="increment">+</button>
<h1>{{ $count }}</h1>
</div>
(Cited: Laravel)
app\Http\Controllers
This directory is home to a range of methods designed to handle various HTTP requests. Laravel simplifies CRUD (Create, Read, Update, Delete) operations, making them straightforward to implement.
app\Http\Controllers\Example.php
<?php
namespace App\Http\Controllers;
use App\Models\Example;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
// Show
public function index(){
// your logic
return view('example.index', compact('name'));
}
// Store
public function store (Request $request){
// your logic
return redirect()->route('example.index',);
}
// Show edit
public function edit (Request $request, $id){
// your logic
return view('example.index', compact('name'));
}
// Update
public function update(Request $request, $id){
// your logic
return redirect()->route('example.index');
}
// Delete
public function destroy($id){
// your logic
return redirect()->route('example.index');
}
}
Another crucial component is routing.
We define our routes in routes\web.php
for web interfaces.
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
(Cited: Laravel)
We can assign a name to each route, which is sometimes necessary. This is referred to as a named route and can be employed for redirection purposes.
While I've covered the foundational elements in this blog article, it's essential to delve into the MVC architecture to gain a deeper understanding of Laravel's inner workings. The basics I've discussed serve as a crucial starting point, and I believe it's important to introduce the MVC concept before proceeding further.
Top comments (0)