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

Free coding bootcamps for beginners
15 Best Free Coding Bootcamps and Resources for Beginners
Soft Skills Training Market
Why "Soft Skills" Training Is The Booming Sector of EdTech? Explore The Growth!
On This Day February 18
On This Day February 18: History, Famous Birthdays, Deaths & Global Events
Best Zoom Alternatives
14 Best Video Conferencing Alternatives to Zoom
psychology of Comfort Movie
The "Comfort Movie" Phenomenon: Why We Rewatch the Same Films!

Fintech & Finance

Family Banking Teaching Kids Financial Literacy with Credit
Family Banking: Teaching Kids Financial Literacy With Credit
safest stablecoins 2026
5 Stablecoins You Can Actually Trust in 2026
Most Innovative Fintech Startups
The 10 Most Innovative Fintech Startups of 2026: The AI & DeFi Revolution
Best alternatives to Revolut and Wise
Top 5 Best Alternatives To Revolut And Wise In 2026
credit cards for airport lounge access
5 Best Cards for Airport Lounge Access in 2026

Sustainability & Living

Ocean Acidification
Unveiling Ocean Acidification: The Silent Killer Of Marine Life!
Indigenous Knowledge In Climate Change
The Role of Indigenous Knowledge In Fighting Climate Change for a Greener Future!
best durable reusable water bottles
Top 6 Reusable Water Bottles That Last a Lifetime
Ethics Of Geo-Engineering
Dive Into The Ethics of Geo-Engineering: Can We Hack the Climate?
Eco-friendly credit cards
7 "Green" Credit Cards That Plant Trees While You Spend

GAMING

best horror games 2026
15 Best Horror Games That Will Actually Scare You in 2026
undergrowthgames custom controller uggcontroman
UnderGrowthGames Custom Controller UggControMan: Unlocking The Gaming Precision!
Upcoming game remakes 2026
7 Remakes And Remasters Confirmed For 2026 Release
The 5 Best VR Headsets Under $500 January 2026 Guide
The 5 Best VR Headsets Under $500: January 2026 Buying Guide
Do Mopfell78 PC Gamers Have An Advantage In Fortnite And Graphic-Intensive PC Games
Do Mopfell78 PC Gamers Have An Advantage in Fortnite And Graphic-Intensive PC Games?

Business & Marketing

digital infusing aggr8tech
Unlocking Efficiency: The Strategic Impact of Digital Infusing Aggr8tech in Modern Enterprises
startup booted fundraising strategy
Beyond the Deck: Building a Startup Bootstrapped Fundraising Strategy That Actually Works
Stocks Betterthisworld
Complete Guide to Purpose-Driven Investing in Stocks Betterthisworld
High-Velocity Logistics 5 Strategies to Avoid Shipping Delays
High-Velocity Logistics: 5 Strategies to Avoid Shipping Delays
charfen.co.uk
Mastering Entrepreneurial Growth: A Strategic Overview of Charfen.co.uk

Technology & AI

Best Zoom Alternatives
14 Best Video Conferencing Alternatives to Zoom
best AI voice generators
10 Best AI Voice Generators for Podcasters and YouTubers
How To Overcome Writer's Block
6 Strategies to Beat "Writer's Block" with AI Assistance: Transform Your Writing!
best ai chatbots customer service
10 Best AI Chatbots for Customer Service Automation
Best Antivirus for Mac
10 Top-Rated Antivirus Suites For Mac Users

Fitness & Wellness

Prerona Roy Transformation
Scars, Science, and Scent: The Profound Rebirth of Prerona Roy
mabs brightstar login
Mastering the MABS Brightstar Login: A Professional Guide to the BrightStar Care ABS Portal
noblu glasses
Noblu Glasses Review: Do They Deliver Effective Blue Light Protection?
The Psychological Cost of Climate Anxiety Coping Mechanisms for 2026
The Psychological Cost of Climate Anxiety: Coping Mechanisms for 2026
Modern Stoicism for timeless wisdom
Stoicism for the Modern Age: Ancient Wisdom for 2026 Problems [Transform Your Life]