Lesson 6 of 50 – Laravel Routes
12%

Laravel Routes and Route Basics

Routes define how Laravel responds when a user visits a URL or sends an HTTP request. A route can return text, display a view, or call a controller method.

1. What Is a Route in Laravel?

A route connects a URL and an HTTP method to the code that should handle the request.

For example, when a visitor opens /about, Laravel can display an About page.

Common route file locations:

  • routes/web.php — browser-based web routes.
  • routes/console.php — console commands and scheduling definitions.
  • routes/api.php — API routes, when API routing has been installed or configured.

In this lesson, we will mainly use routes/web.php.

The API route file may not exist in a fresh Laravel installation. In Laravel versions that support it, you can install API routing with php artisan install:api.

2. Create Your First Route

Open the routes/web.php file in your Laravel project.

Example:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/hello', function () {
    return 'Hello, Welcome to Laravel!';
});

Start the development server:

php artisan serve

Open the following URL in your browser:

http://127.0.0.1:8000/hello

The browser will display:

Hello, Welcome to Laravel!

Here, Route::get() registers a route that responds to an HTTP GET request.

3. Different HTTP Route Methods

Laravel provides route methods for different HTTP request types.

Method Purpose
Route::get() Retrieve or display information.
Route::post() Submit data, such as a form.
Route::put() Replace or update a resource.
Route::patch() Partially update a resource.
Route::delete() Delete a resource.

Examples:

Route::get('/students', function () {
    return 'Display Students';
});

Route::post('/students', function () {
    return 'Save Student';
});

Route::put('/students/1', function () {
    return 'Update Student';
});

Route::patch('/students/1', function () {
    return 'Update Some Student Fields';
});

Route::delete('/students/1', function () {
    return 'Delete Student';
});

These examples demonstrate route definitions only. In a real application, use appropriate controllers, validation, authentication, and authorization.

4. Return Text and HTML from Routes

A route can return a simple text response.

Route::get('/welcome', function () {
    return 'Welcome to Soopro Pathshala';
});

You can also return a short HTML response:

Route::get('/contact-info', function () {
    return '<h1>Contact Us</h1>
            <p>Welcome to our website.</p>';
});

For complete pages, Laravel views are generally a better choice than writing large amounts of HTML directly inside route closures.

5. Display a Blade View Using a Route

Laravel uses Blade templates to create reusable HTML pages.

Create a file named about.blade.php inside the resources/views/ directory.

File: resources/views/about.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>About Us</title>
</head>
<body>
    <h1>About Soopro Pathshala</h1>
    <p>Learn programming step by step.</p>
</body>
</html>

Now add this route in routes/web.php:

Route::get('/about', function () {
    return view('about');
});

Visit http://127.0.0.1:8000/about to display the page.

Notice that view('about') uses the view name without the .blade.php extension.

6. Route Parameters

Route parameters allow you to receive values directly from the URL.

Example: Student ID

Route::get('/student/{id}', function ($id) {
    return 'Student ID: ' . $id;
});

Open:

http://127.0.0.1:8000/student/101

Output:

Student ID: 101

Optional route parameter:

Route::get('/greeting/{name?}', function ($name = 'Student') {
    return 'Hello, ' . $name;
});

Both /greeting and /greeting/Rahul can match this route.

Optional parameters must have a default value in the route callback or controller method.

7. Multiple Parameters and Constraints

A route can contain more than one parameter.

Route::get('/student/{id}/course/{course}', function ($id, $course) {
    return 'Student ID: ' . $id . ', Course: ' . $course;
});

Example URL:

/student/101/course/PHP

Restrict a parameter to digits:

Route::get('/student/{id}', function ($id) {
    return 'Student ID: ' . $id;
})->whereNumber('id');

The whereNumber() constraint allows numeric values for the specified parameter.

Restrict a parameter using a pattern:

Route::get('/category/{name}', function ($name) {
    return 'Category: ' . $name;
})->where('name', '[A-Za-z]+');

If a URL does not satisfy the constraint, this route will not match it.

8. Route Redirects, Fallbacks, and Route List

Redirect one URL to another:

Route::redirect('/old-about', '/about');

By default, this shortcut creates a temporary redirect. You can also specify a status code.

Route::redirect('/old-page', '/about', 301);

Create a fallback route:

Route::fallback(function () {
    return response('Page Not Found', 404);
});

A fallback route handles requests that do not match any registered route. Define it after your other web routes.

List registered routes in the terminal:

php artisan route:list

To see more route details, try:

php artisan route:list -v

If a newly added route does not appear as expected, check the route file, clear the route cache if necessary, and run route:list again.

9. Practical Assignment

Create the following routes in your routes/web.php file.

  1. Create a /home route that displays a welcome message.
  2. Create an /about route that displays a Blade view.
  3. Create a /contact route that returns contact information.
  4. Create a /student/{id} route that displays a student ID.
  5. Create a /course/{name} route that displays a course name.
  6. Create a /greeting/{name?} route with a default name.
  7. Restrict a route parameter to numeric values.
  8. Create a redirect from /old-home to /home.
  9. Use php artisan route:list to inspect your routes.

Remember to use unique URL paths for each exercise and avoid duplicate route definitions that conflict with one another.


🧠 Quick Quiz

1. Which file usually contains Laravel web routes?




2. Which method defines a GET route?




3. How do you display a Blade view named about?