This guide will walk you through creating your first Laravel application, from setting up routes to displaying data in views.
Prerequisites
Before you begin, make sure you have:
This guide assumes you’ve completed the installation and have a fresh Laravel project ready.
Start the development server
First, navigate to your Laravel project and start the development server:
cd example-app
php artisan serve
Your application will be available at http://localhost:8000.
Understanding the default route
Laravel includes a default route in routes/web.php. Let’s examine it:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
This route:
- Responds to GET requests at the
/ URL
- Returns the
welcome view (located at resources/views/welcome.blade.php)
Create your first route
Let’s create a simple route that displays a greeting.
Add a new route
Open routes/web.php and add a new route:Route::get('/hello', function () {
return 'Hello, Laravel!';
});
Test the route
Visit http://localhost:8000/hello in your browser. You should see “Hello, Laravel!”.
Working with route parameters
Routes can accept dynamic parameters:
Route::get('/hello/{name}', function ($name) {
return "Hello, {$name}!";
});
Now visit http://localhost:8000/hello/John to see a personalized greeting.
Route parameters are automatically injected into your closure or controller method.
Creating views
Instead of returning strings, let’s create proper views using Blade templates.
Create a view file
Create a new file at resources/views/greeting.blade.php:resources/views/greeting.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Greeting</title>
</head>
<body>
<h1>Hello, {{ $name }}!</h1>
<p>Welcome to Laravel.</p>
</body>
</html>
The .blade.php extension tells Laravel to process this file with the Blade templating engine.
Update the route to use the view
Modify your route to return the view with data:Route::get('/hello/{name}', function ($name) {
return view('greeting', ['name' => $name]);
});
Test the view
Visit http://localhost:8000/hello/Laravel to see your new view in action.
Using Blade templates
Blade provides powerful templating features. Let’s explore some common patterns:
Variables and echoing
{{ $name }} <!-- Escaped output -->
{!! $html !!} <!-- Unescaped output -->
Conditional statements
@if ($name === 'Laravel')
<p>Welcome back, Laravel!</p>
@else
<p>Hello, {{ $name }}!</p>
@endif
Loops
@foreach ($users as $user)
<li>{{ $user->name }}</li>
@endforeach
Creating a controller
As your application grows, you’ll want to move logic from routes into controllers.
Generate a controller
Use Artisan to create a new controller:php artisan make:controller GreetingController
This creates a new file at app/Http/Controllers/GreetingController.php. Add a method to the controller
Open the controller and add a method:app/Http/Controllers/GreetingController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class GreetingController extends Controller
{
public function show($name)
{
return view('greeting', ['name' => $name]);
}
}
Update the route to use the controller
Modify your route in routes/web.php:use App\Http\Controllers\GreetingController;
Route::get('/hello/{name}', [GreetingController::class, 'show']);
Understanding the Artisan CLI
Artisan is Laravel’s command-line interface. It provides helpful commands for development:
#!/usr/bin/env php
<?php
use Illuminate\Foundation\Application;
use Symfony\Component\Console\Input\ArgvInput;
define('LARAVEL_START', microtime(true));
// Register the Composer autoloader...
require __DIR__.'/vendor/autoload.php';
// Bootstrap Laravel and handle the command...
$app = require_once __DIR__.'/bootstrap/app.php';
$status = $app->handleCommand(new ArgvInput);
exit($status);
Useful Artisan commands
# Start development server
php artisan serve
# Clear application cache
php artisan cache:clear
# Clear configuration cache
php artisan config:clear
# View all routes
php artisan route:list
Run php artisan without arguments to see all available commands.
Application bootstrap
Laravel’s application bootstrap process is defined in bootstrap/app.php:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
//
})
->withExceptions(function (Exceptions $exceptions): void {
//
})->create();
This configuration:
- Loads web routes from
routes/web.php
- Loads console commands from
routes/console.php
- Provides a
/up health check endpoint
- Allows middleware and exception handling customization
Building a complete example
Let’s build a simple “About” page with multiple sections.
Create the route
Add to routes/web.php:Route::get('/about', function () {
return view('about', [
'title' => 'About Laravel',
'features' => [
'Simple, fast routing engine',
'Powerful dependency injection container',
'Expressive, intuitive database ORM',
'Database agnostic schema migrations',
'Robust background job processing',
],
]);
});
Create the view
Create resources/views/about.blade.php:resources/views/about.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
<style>
body {
font-family: sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
}
h1 { color: #FF2D20; }
ul { line-height: 1.8; }
</style>
</head>
<body>
<h1>{{ $title }}</h1>
<p>Laravel is a web application framework with expressive, elegant syntax.</p>
<h2>Key Features</h2>
<ul>
@foreach ($features as $feature)
<li>{{ $feature }}</li>
@endforeach
</ul>
<p><a href="/introduction">Back to home</a></p>
</body>
</html>
Test the page
Visit http://localhost:8000/about to see your new page.
Next steps
Congratulations! You’ve created your first Laravel routes, views, and controller. Here’s what to explore next:
Routing
Learn advanced routing techniques and route groups
Blade templates
Master Blade’s templating features and components
Database
Connect to a database and use Eloquent ORM
Validation
Validate user input and handle form submissions
Remember to set APP_DEBUG=false in your .env file when deploying to production.