0
0
Laravelframework~5 mins

Basic route definition in Laravel

Choose your learning style9 modes available
Introduction

Routes tell your Laravel app which code to run when someone visits a web address. They connect URLs to actions.

When you want to show a page when someone visits a URL.
When you want to handle a form submission from a user.
When you want to create an API endpoint to send or receive data.
When you want to organize your app's URLs clearly.
When you want to control what happens for different web addresses.
Syntax
Laravel
Route::get('/url', function () {
    return 'Response';
});
Use Route::get() for URLs accessed with a browser or GET request.
The first argument is the URL path, the second is the code to run.
Examples
This shows a welcome message when visiting the home page.
Laravel
Route::get('/', function () {
    return 'Welcome to my site!';
});
This handles a form sent with POST to /submit.
Laravel
Route::post('/submit', function () {
    return 'Form submitted!';
});
This returns a view file named 'about' when visiting /about.
Laravel
Route::get('/about', function () {
    return view('about');
});
Sample Program

This route shows the text 'Hello, Laravel!' when you visit the home page URL.

Laravel
<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return 'Hello, Laravel!';
});
OutputSuccess
Important Notes

Routes are defined in the routes/web.php file for web pages.

Use Route::post() for handling form data sent securely.

Always clear your route cache if routes don't update: run php artisan route:clear.

Summary

Routes connect URLs to code that runs when those URLs are visited.

Use Route::get() for simple page responses.

Routes help organize your app's web addresses clearly and simply.