Imagine walking into a large office building. At the front entrance, there is a helpful receptionist. You say, "I am here for the meeting," and the receptionist points you to Room 204. In the web development world, Routes are that welcoming receptionist!
What is a Route in Plain English?
Every time you type a web address into your browser (like example.com/about) and hit Enter, your browser sends a request to the web server. A route is a simple instruction that tells Laravel: "When someone visits this specific address, show them this page or run this action."
Your Very First Route in Laravel
In Laravel, web routes live inside a file called routes/web.php. Here is how simple it is to return a friendly greeting to any visitor:
// When a visitor goes to the homepage ('/'), run this simple function
Route::get('/', function () {
// Return a welcoming message to the visitor screen
return 'Welcome to my website!';
});
Sending Visitors to a View Page
Instead of sending plain text, you will usually want to show a nice HTML page (called a View). Laravel makes this a breeze:
// When a visitor types '/about' in their browser address bar
Route::get('/about', function () {
// Display the welcome.blade.php template file to the user
return view('welcome');
});
3 Things to Remember
- Routes are street signs: They guide visitors from an address bar to the correct response.
- Stored in routes/web.php: This is where all your website navigation rules live.
- Start small: A basic route needs just a URL path and a simple return value.