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

How to Find Reliable Toy Collection Shops
How to Find Reliable Toy Collection Shops?
ralph macchio daughter
Meet Julia Macchio: Ralph Macchio's Daughter and Rising Actress
M6.6 Earthquake Western Japan 12 Injured
M6.6 Earthquake Strikes Western Japan, 12 Injured
electric mini car for adults
Electric Mini Car for Adults in 2024: Affordable and Stylish Options
Ashanti Nelly Engaged Expecting Baby
Ashanti and Nelly Engaged, Expecting Their First Child Together

LIFESTYLE

Gift Ideas for Men
10 Thoughtful and Unique Gift Ideas for Men Who Have Everything
pohela boishakh 2024
Pohela Boishakh: Celebrating Bengali Culture and Heritage Festivities
Korean Beauty Secrets
10 Korean Beauty Secrets for Youthful Energy: Stay Young & Vibrant
Ancient Philosophers Guide to Happiness
Unlocking Happiness: Timeless Lessons from Ancient Philosophers
eid decor diy
Eid Decor DIY: 15 Creative Ideas to Spruce Up Your Home for the Festivities

Entertainment

ralph macchio daughter
Meet Julia Macchio: Ralph Macchio's Daughter and Rising Actress
Ashanti Nelly Engaged Expecting Baby
Ashanti and Nelly Engaged, Expecting Their First Child Together
Taylor Swift Tokyo Tour Super Bowl
Everything to Know About Taylor Swift’s New Album 'The Tortured Poets Department'
best comedies on peacock
15 Best Comedies on Peacock to Watch in 2024 [Top-Rated Comedies]
Popular VTubers
Unveiling the Stars: Exploring the World of Popular VTubers - A Comprehensive Guide

GAMING

Mobile Sports Betting Apps in the US
The Surge of Mobile Sports Betting Apps in the US, Benefits and Drawbacks
Pokemon Go Updates Avatars Maps Photos
Pokemon Go Update: New Changes to Avatars, Map, Photos & More
Apple's First Approved iPhone Emulator Launches
Apple's First Approved iPhone Emulator Launches, Then Gets Removed
Prime Gaming
Is 2024 the Year Prime Gaming Takes Off?
Online Games for Stress Relief
Finding Calm in the Click: A Comparative Look at Online Games for Stress Relief

BUSINESS

Elon Musk India Tour 2024 Itinerary
Elon Musk Historic India Visit 2024 Itinerary: The Agendas So Far
Douglas Palermo
Financial Status of Douglas Palermo After His Bankruptcy [2024 Update]
Choose the Right ERP System
How to Choose the Right ERP System for Your Business?
Apple CEO Discusses Investment Plans
Apple CEO Discusses Investment Plans with Indonesian Leader
DEI Strategy
A Roadmap for Implementing and Sustaining a DEI Strategy

TECHNOLOGY

electric mini car for adults
Electric Mini Car for Adults in 2024: Affordable and Stylish Options
How to Use Technology Mindfully
How to Use Technology Mindfully: Essential Tips for Balanced Tech
Quick Ways to Remove a Trojan From Your Mac
Quick Ways to Remove a Trojan From Your Mac
Pixel Launcher Upgrades in Android 15
Explore the Latest Pixel Launcher Upgrades in Android 15!
Google One VPN Ends
Google One VPN Ends, Pixel VPN Upgrades Coming Soon!

HEALTH

why veterans choose Out of Town Rehab
Finding Freedom: Why Veterans Should Opt for Out-of-Town Rehab?
Rock Hudson Last Days
Rock Hudson's Last Days: The Untold Story of His Final Moments
Best Stress Relief for Each Zodiac Sign
Relaxation by the Stars: Best Stress Relief for Each Zodiac Sign
Covid 19 No Link Asthma Risk Study
COVID-19 Does Not Raise Asthma Risk, Researchers Confirm
Williams Syndrome Famous People
5 Famous People in the World Dealing With Williams Syndrome [2024 Update]