Search
Close this search box.
Search
Close this search box.

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

Crypto Celebrities Who Are Changing The Game
10 Crypto Celebrities Who Are Changing The Game
VPS Hosts Offering Flexible And Scalable Storage Upgrades
Top 10 VPS Hosts Offering Flexible And Scalable Storage Upgrades
Strategies For Sustainable Logistics Practices
Top 10 Strategies For Sustainable Logistics Practices
Tips For Maximizing Natural Light In Your Home
8 Designer Tips For Maximizing Natural Light In Your Home
Steps To Buying A Property In Italy’s
12 Steps To Buying A Property In Italy’s Truffle Regions

LIFESTYLE

lovelolablog code
Unlock Exclusive Lovelolablog Code For Discount Deals in 2025
Sustainable Kiwi Beauty Products
10 Sustainable Kiwi Beauty Products You Should Try for a Greener Routine
Best E-Bikes for Seniors
Best E-Bikes for Seniors with Comfort and Safety in Mind
wellhealthorganic.com effective natural beauty tips
Top 5 Well Health Organic Beauty Tips for Glowing Skin
Chemical-Free Alternatives to Your Favorite Beauty Products
7 Chemical-Free Alternatives to Your Favorite Beauty Products You Need to Try

Entertainment

The Story of Slash Electric Alexander Edwards
Meet Cher's Boyfriend's Son: The Story of Slash Electric Alexander Edwards
Ben Shapiro Net Worth
Ben Shapiro Net Worth: How He Made a Fortune of $50 Million
jynxzi net worth
Jynxzi Net Worth 2025: Breaking Down The Streamer's Earnings, Assets, and Lifestyle
Best Korean Series
5 Best Korean Series to Watch on Netflix in 2025
Piper Rockelle lawsuit update
Piper Rockelle Lawsuit Update: $1.85M Deal & Allegations Explained

GAMING

How To Grow Your Gaming Channel On YouTube
Want to Skyrocket Your YouTube Gaming Channel? Here's How!
harmonicode games
Exciting Harmonicode Games for Gamers: Dive Into the Ultimate Gaming Experience!
thinkofgames com guides
Ultimate ThinkOfGames Com Guides: The Ultimate Resource For Gamers
Ways Gaming Can Actually Improve Your Mental Health
Top 10 Ways Gaming Can Actually Improve Your Mental Health
Stay Productive While Working from Home
10 Practical Ways to Stay Productive While Working from Home

BUSINESS

Strategies For Sustainable Logistics Practices
Top 10 Strategies For Sustainable Logistics Practices
Video Marketing for Brand Recognition
The Power of Video Marketing for Brand Recognition
Ways Businesses Can Integrate Web3 Without Overhauling Everything
7 Web3 Integration Tips for Businesses That Want Simplicity, Not Chaos
Elon Musk Trump tariff pause tesla stock soars
Elon Musk Wins as Trump Halts Tariffs, Tesla Stock Soars
Australian Platforms Offering Live Mentorship
10 Australian Platforms Offering Live Mentorship And 24/7 Support

TECHNOLOGY

Ways Businesses Can Integrate Web3 Without Overhauling Everything
7 Web3 Integration Tips for Businesses That Want Simplicity, Not Chaos
How Quantum Computing Could Disrupt the Web3 Space
Web3 Revolution: Brace for Quantum Computing Disruption
Web3 Loyalty Programs Rewarding Users For Engagement
Get Rewarded for Engagement with These Top Web3 Loyalty Programs
Best Cameras For Streaming High-Quality Gameplay
Upgrade Your Stream: 7 Best Cameras for Streaming High-Quality Gameplay
Best Mobile-First Learning Apps For Australians
Top 10 Best Mobile-First Learning Apps For On-the-Go Australians

HEALTH

Small Things Neurologists Wish You’d Do For Your Brain
10 Small Things Neurologists Wish You’d Do For Your Brain
Ways Gaming Can Actually Improve Your Mental Health
Top 10 Ways Gaming Can Actually Improve Your Mental Health
Benefits of Single Case Agreements for Patients
Benefits of Single Case Agreements for Patients & Providers
World Health Day 2025
Exploring the Significance of World Health Day 2025
Telemedicine Tips
Telemedicine Tips: Scheduling Virtual Care Right