Laravel routing is one of the most important parts of a Laravel application.
Routing decides what happens when a user opens a URL in the browser.
Let’s understand routing step by step in very simple words.
What is Routing?
Routing means connecting a URL to some code.
Example:
When a user opens this URL in the browser:
https://example.com/about
Laravel needs to know:
- What page to show?
- Which code to run?
This decision is made by routes.
So, routing = telling Laravel what to do for a specific URL.
web.php Explained
In Laravel, routes are stored inside the routes folder.
The most common file is:
routes/web.php
This file is used for:
- Website pages
- Forms
- Pages that users see in the browser
Example of a simple route in web.php:
Route::get('/', function () {
return "Welcome to Laravel";
});
This means:
- When someone opens the home page
/ - Laravel shows the text “Welcome to Laravel”
Basic GET Routes
A GET route is used to show data or pages.
Example:
Route::get('/about', function () {
return "This is the About page";
});
When the user opens:
/about
Laravel shows:
This is the About page
GET routes are mostly used for:
- Pages
- Reading data
- Showing content
Basic POST Routes
A POST route is used to send data to the server.
This is commonly used in:
- Forms
- Login pages
- Contact forms
Example:
Route::post('/submit-form', function () {
return "Form submitted successfully";
});
This route:
- Accepts form data
- Processes it on the server
POST routes are used when:
- Data needs to be saved
- Data is sent securely
Route Parameters
Route parameters allow us to send values through the URL.
Example:
Route::get('/user/{name}', function ($name) {
return "Hello " . $name;
});
If the user opens:
/user/John
Laravel will show:
Hello John
Here:
{name}is a parameter$namereceives the value from the URL
Route parameters are useful for:
- User profiles
- Blog posts
- Dynamic pages
Summary
- Routing connects URLs to code
web.phpcontains website routes- GET routes are used to show pages
- POST routes are used to send data
- Route parameters make URLs dynamic
Routing helps Laravel understand what to do and when to do it.
1 Likes 0 Comments 8 Views
Leave a comment
Leave a Reply