Simple Laravel application with a complete view, including a route, controller, and a Blade template.
Step 1: Create a New Laravel Project
Open your terminal and run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel laravel_quick_view
Step 2: Create a Controller
Create a controller named ‘WelcomeController’ using the following Artisan command:
php artisan make:controller WelcomeController
Edit the controller file located at app/Http/Controllers/WelcomeController.php: and paste this code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class WelcomeController extends Controller
{
public function index()
{
$name = 'John Doe';
return view('welcome', ['name' => $name]);
}
}
?>
Step 3: Create a Blade View
Create a blade view file ‘welcome.blade.php’ in the resources/views directory:
@extends('layouts.app')
@section('title', 'Welcome Page')
@section('content')
<div class="container">
<h1>Hello, {{ $name }}!</h1>
</div>
@endsection
Step 4: Define a Route
Open the routes/web.php file and define a route that points to the index method of the WelcomeController:
<?php
use App\Http\Controllers\WelcomeController;
Route::get('/welcome', [WelcomeController::class, 'index']);
?>
Step 5: Blade Layout
Create a Blade layout file named app.blade.php in the resources/views/layouts directory:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title', 'Default Title')</title>
</head>
<body>
@yield('content')
</body>
</html>
Step 6: Run the Application
Run the development server using the following command:
php artisan serve or php -S localhost:8001 -t public
Visit http://localhost:8000/welcome in your browser. You should see the welcome message.