What to Create The Basic Laravel Routes?

Laravel

In Laravel, every post put, and delete request has an accompanying CSRF token that needs to be given along with the request. If this is not remedied, the request will be denied. The official documentation for Laravel contains further information regarding CSRF prevention.

A Basic GET Route

You may construct a GET request route for /table using the code that is below; doing so will cause the table to be displayed on the screen.

Route::get(‘/table’, function () {

   for($i =1; $i <= 10 ; $i++){

       echo “$i * 2 = “. $i*2 .”<br>”;

   }   

});

Now, let’s say you want the user to select the number of copies of the table that should be printed by the route. The following is the routing code for such a path:

Route::get(‘/table/{number}’, function ($number) {

   for($i =1; $i <= 10 ; $i++){

       echo “$i * $number = “. $i* $number .”<br>”;

   }   

});

A route variable was created and provided to the route function in the preceding code. Any time a user navigates to /table and includes a number in the request, the function will now output the table corresponding to that number. 

Indeed! Optional parameters are supported in Laravel, and you will be able to implement them by appending a question mark (?) to the end of the parameter’s name and the default value. Here’s what you’ll do for the preceding route example:

Route::get(‘/table/{number?}’, function ($number = 2) {

   for($i =1; $i <= 10 ; $i++){

       echo “$i * $number = “. $i* $number .”<br>”;

   }   

});

If you face error, you can also check How to fix “The POST method is not supported for this route. Supported methods: GET, HEAD”.

Regular Expression Constraints For Route Parameters

Up above, we established a path for a table, but how can we check if the parameter passed along that path is, in fact, a numeric value so that we don’t encounter any errors while constructing the table?

When creating a route in Laravel, you can use the ‘where’ method on the route instance to establish a restriction for the route’s parameters. The where method accepts a parameter name and a regular expression rule specific to that parameter. First, let’s set a requirement that the function accepts only the value “number” for the “number” parameter.

Route::get(‘/table/{number?}’, function ($number = 2) {

   for($i =1; $i <= 10 ; $i++){

       echo “$i * $number = “. $i* $number .”<br>”;

   }   

})->where(‘number’, ‘[0-9]+’);

You can also define a regular expression for our route number in the preceding code snippet. A NotFoundHttpException will now be shown if the user attempts to route to the /table/no path.

Laravel Routing with Controller Function

In Laravel, it is possible to assign a controller method to a route. In the presence of a controller method, whenever a user visits the route, the defined method of the controller will execute the actions.

Use the following code snippet to assign a controller method to the route.

Route::get(‘/home’, ‘YourController@functionname’);

The code begins with ‘Route::’ and defines the route’s request method. Next, describe your route, followed by the Controller and the method you wish to tie to this route by prefixing the method’s name with the @ sign.

Naming Your Route

You can define the name of your route in Laravel. This moniker is frequently useful in a variety of situations. If you wish to redirect a user from one route to another, for instance, you do not need to define the complete redirect URL. Instead, you need to only provide its name for the code to function! Using the ‘name’ method on the route object, you may define the route’s name.

Route::get(‘/table/{number?}’, function ($number = 2) {

   for($i =1; $i <= 10 ; $i++){

       echo “$i * $number = “. $i* $number .”<br>”;

   }   

})->where(‘number’, ‘[0-9]+’)->name(‘table’);

Now, I am able to regenerate the URL for this route by:

$url = route(‘table’);

Similarly, the correct syntax for redirecting to this URL would be:

return redirect()->route(‘table’);

Conclusion

We have discussed the Laravel 5.5 route. I presented other related ideas pertaining to routes, including regular expressions and route names. You might also refer to the official Laravel routing docs to learn more about routing.


Subscribe to Our Newsletter

Related Articles

Top Trending

Bangladesh T20 Venue Dispute
Beyond The Headlines: Bangladesh T20 Venue Dispute And The Geopolitics Behind Avoiding Indian Venues For The 2026 T20 World Cup
AI Augmented Office
Beyond The Copilot Hype: What The AI-Augmented Office Means For Employee Identity In 2026
Samsung AI chip profit jump
The $1 Trillion Chip Race: How Samsung’s 160% Profit Jump Validates the AI Hardware Boom
Invisible AI
The Rise of "Invisible AI": How Ambient Technology is Reshaping Sustainable Home Living in 2026
Quantum Ready Finance
Beyond The Headlines: Quantum-Ready Finance And The Race To Hybrid Cryptographic Frameworks

LIFESTYLE

Benefits of Living in an Eco-Friendly Community featured image
Go Green Together: 12 Benefits of Living in an Eco-Friendly Community!
Happy new year 2026 global celebration
Happy New Year 2026: Celebrate Around the World With Global Traditions
dubai beach day itinerary
From Sunrise Yoga to Sunset Cocktails: The Perfect Beach Day Itinerary – Your Step-by-Step Guide to a Day by the Water
Ford F-150 Vs Ram 1500 Vs Chevy Silverado
The "Big 3" Battle: 10 Key Differences Between the Ford F-150, Ram 1500, and Chevy Silverado
Zytescintizivad Spread Taking Over Modern Kitchens
Zytescintizivad Spread: A New Superfood Taking Over Modern Kitchens

Entertainment

Stranger Things Finale Crashes Netflix
Stranger Things Finale Draws 137M Views, Crashes Netflix
Demon Slayer Infinity Castle Part 2 release date
Demon Slayer Infinity Castle Part 2 Release Date: Crunchyroll Denies Sequel Timing Rumors
BTS New Album 20 March 2026
BTS to Release New Album March 20, 2026
Dhurandhar box office collection
Dhurandhar Crosses Rs 728 Crore, Becomes Highest-Grossing Bollywood Film
Most Anticipated Bollywood Films of 2026
Upcoming Bollywood Movies 2026: The Ultimate Release Calendar & Most Anticipated Films

GAMING

High-performance gaming setup with clear monitor display and low-latency peripherals. n Improve Your Gaming Performance Instantly
Improve Your Gaming Performance Instantly: 10 Fast Fixes That Actually Work
Learning Games for Toddlers
Learning Games For Toddlers: Top 10 Ad-Free Educational Games For 2026
Gamification In Education
Screen Time That Counts: Why Gamification Is the Future of Learning
10 Ways 5G Will Transform Mobile Gaming and Streaming
10 Ways 5G Will Transform Mobile Gaming and Streaming
Why You Need Game Development
Why You Need Game Development?

BUSINESS

Samsung AI chip profit jump
The $1 Trillion Chip Race: How Samsung’s 160% Profit Jump Validates the AI Hardware Boom
Embedded Finance 2.0
Embedded Finance 2.0: Moving Invisible Transactions into the Global Education Sector
HBM4 Supercycle
The Great Silicon Squeeze: How the HBM4 "Supercycle" is Cannibalizing the Chip Market
South Asia IT Strategy 2026: From Corridor to Archipelago
South Asia’s Silicon Corridor: How Bangladesh & India are Redefining Regionalized IT?
Featured Image of Modernize Your SME
Digital Business Blueprint 2026, SME Modernization, Digital Transformation for SMEs

TECHNOLOGY

AI Augmented Office
Beyond The Copilot Hype: What The AI-Augmented Office Means For Employee Identity In 2026
Samsung AI chip profit jump
The $1 Trillion Chip Race: How Samsung’s 160% Profit Jump Validates the AI Hardware Boom
Quantum Ready Finance
Beyond The Headlines: Quantum-Ready Finance And The Race To Hybrid Cryptographic Frameworks
Solid-State EV Battery Architecture
Beyond Lithium: The 2026 Breakthroughs in Solid-State EV Battery Architecture
AI Integrated Labs
Beyond The Lab Report: What AI-Integrated Labs Mean For Clinical Medicine In 2026

HEALTH

Digital Detox for Kids
Digital Detox for Kids: Balancing Online Play With Outdoor Fun [2026 Guide]
Worlds Heaviest Man Dies
Former World's Heaviest Man Dies at 41: 1,322-Pound Weight Led to Fatal Kidney Infection
Biomimetic Brain Model Reveals Error-Predicting Neurons
Biomimetic Brain Model Reveals Error-Predicting Neurons
Long COVID Neurological Symptoms May Affect Millions
Long COVID Neurological Symptoms May Affect Millions
nipah vaccine human trial
First Nipah Vaccine Passes Human Trial, Shows Promise