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

What Travel Insurance From a Credit Card Usually Covers
Travel Insurance Benefits: What Your Card Actually Covers In 2026
leadership skills in 2026
10 Leadership Skills You Must Master in 2026
monster hunter wilds support cross-platform
Is Monster Hunter Wilds Cross-Platform? PC, PS5, And Xbox Compatibility
On This Day January 28
On This Day January 28: History, Famous Birthdays, Deaths & Global Events
monster hunter wilds release
When Did Monster Hunter Wilds Release? Launch Date And Update History

Fintech & Finance

What Travel Insurance From a Credit Card Usually Covers
Travel Insurance Benefits: What Your Card Actually Covers In 2026
Gold vs Bitcoin Investment
The Great Decoupling: Why Investors Are Choosing Bullion Over Blockchain in 2026
Why Customer Service is the Battleground for Neobanks in 2026
Why Customer Service is the Battleground for Neobanks in 2026
cryptocurrencies to watch in January 2026
10 Top Cryptocurrencies to Watch in January 2026
best travel credit cards for 2026
10 Best Travel Credit Cards for 2026 Adventures

Sustainability & Living

Tiny homes
Tiny Homes: A Solution to Homelessness or Poverty with Better Branding?
Smart Windows The Tech Saving Energy in 2026 Skyscrapers
Smart Windows: The Tech Saving Energy in 2026 Skyscrapers
The Environmental Impact of Recycling Solar Panels
The Environmental Impact Of Recycling Solar Panels
Renewable Energy Trends
Top 10 Renewable Energy Trends Transforming the Power Sector in 2026
Eco-Friendly Building Materials
10 Top Trending Eco-Friendly Building Materials in 2026

GAMING

monster hunter wilds support cross-platform
Is Monster Hunter Wilds Cross-Platform? PC, PS5, And Xbox Compatibility
monster hunter wilds release
When Did Monster Hunter Wilds Release? Launch Date And Update History
monster hunter wilds monster list
Monster Hunter Wilds Monster List: Every Large Monster & Variant
Esports Fatigue How Leagues Are reinventing Viewership for Gen Alpha
Esports Fatigue: How Leagues Are Reinventing Viewership For Gen Alpha
Exploring the Future of Online Gaming How New Platforms Are Innovating
Exploring the Future of Online Gaming: How New Platforms Are Innovating

Business & Marketing

leadership skills in 2026
10 Leadership Skills You Must Master in 2026
Billionaire Wealth Boom
Billionaire Wealth Boom: Why 2025 Was The Best Year In History For Billionaires
ESourcing Software The Complete Guide for Businesses
ESourcing Software: The Complete Guide for Businesses
The End of the Seat-Based License How AI Agents are Changing Pricing
The End of the "Seat-Based" License: How AI Agents are Changing Pricing
Best Citizenship by Investment Programs
The "Paper Ceiling": Why a Second Passport is No Longer a Luxury, But an Economic Survival Kit for the Global South

Technology & AI

zero-water data centers
The “Thirsty” Cloud: How 2026 Became the Year of Zero-Water Data Centers and Sustainable AI
The End of the Seat-Based License How AI Agents are Changing Pricing
The End of the "Seat-Based" License: How AI Agents are Changing Pricing
the Great AI Collapse
The Great AI Collapse: What the GPT-5.2 and Grokipedia Incident Actually Proves
green web hosting providers
10 Best Green Web Hosting Providers for 2026
Blockchain gas fees explained
Blockchain Gas Fees Explained: Why You Pay Them and How to Lower Transaction Costs

Fitness & Wellness

Mental Health First Aid for Managers
Mental Health First Aid: A Mandatory Skill for 2026 Managers
The Quiet Wellness Movement Reclaiming Mental Focus in the Hyper-Digital Era
The “Quiet Wellness” Movement: Reclaiming Mental Focus in the Hyper-Digital Era
Cognitive Optimization
Brain Health is the New Weight Loss: The Rise of Cognitive Optimization
The Analogue January Trend Why Gen Z is Ditching Screens for 30 Days
The "Analogue January" Trend: Why Gen Z is Ditching Screens for 30 Days
Gut Health Revolution The Smart Probiotic Tech Winning CES
Gut Health Revolution: The "Smart Probiotic" Tech Winning CES