Create a project with composer create-project laravel/laravel my-app, then start it with php artisan serve and open http://localhost:8000.
Your first routes
Each route has an HTTP method, a URL and the code that returns the response.
<?php
// routes/web.php
use Illuminate\Support\Facades\Route;
Route::get('/hello', function () {
return 'Hello from Laravel!';
});
// {id} is a route parameter: /students/7 gives $id = '7'
Route::get('/students/{id}', function (string $id) {
return "Student number $id";
});Controllers
Closures are fine for tiny apps, but real code belongs in controllers. Create one with php artisan make:controller LessonController, then point a route at one of its methods.
Route::getanswers page visits;Route::postanswers form submissions.php artisan route:listshows every route your app knows.- Give a route a name with
->name('lessons.show')and link to it withroute('lessons.show', $slug).
Code examples
<?php
// app/Http/Controllers/LessonController.php
namespace App\Http\Controllers;
class LessonController extends Controller
{
public function show(string $slug)
{
return "You are reading: $slug";
}
}
// routes/web.php
use App\Http\Controllers\LessonController;
Route::get('/lessons/{slug}', [LessonController::class, 'show']);Expected output
Visiting /lessons/php-basics shows: You are reading: php-basics
Laravel passes the {slug} part of the URL to the show method's $slug parameter.
Common mistakes
Common mistake: “Target class [LessonController] does not exist”
Import the controller at the top of routes/web.php: use App\Http\Controllers\LessonController;
Common mistake: A 404 for a route you just wrote
Check the method and URL with php artisan route:list. A form that sends POST needs Route::post, not Route::get.
Check your understanding
3 quick questions about this lesson.