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

Christina Yamamoto
Christina Yamamoto: The Life and Legacy of Jhené Aiko's Mother
Rhea Ripley Husband Revealed
Rhea Ripley Husband Revealed: The Story of Her Journey With Buddy Matthews
Gaming Updates LCFModGeeks
Gaming Updates LCFModGeeks: Stay Ahead With Modded Software and Gamer Content
How Tall is Michael J Fox
How Tall is Michael J Fox? The Truth About His Height Revealed
Is Michael J. Fox a Christian
Is Michael J. Fox a Christian? Exploring the Actor's Views on Religion and Faith

LIFESTYLE

Clean Beauty Movement
How the Clean Beauty Movement Is Transforming Skincare in 2025
Gender Reveal Balloons
The Ultimate Guide to Gender Reveal Balloons: Colors, Styles, and Surprises
Best Places to Shop in Manchester
Shop 'Til You Drop: The Best Places to Shop in Manchester for Every Style
retirement cities in California
10 Best Retirement Cities in California for a Relaxed and Affordable Life
Mother's Day Around The World
Mother’s Day Traditions Around the World: Mother's Day 2025 Special

Entertainment

Christina Yamamoto
Christina Yamamoto: The Life and Legacy of Jhené Aiko's Mother
Rhea Ripley Husband Revealed
Rhea Ripley Husband Revealed: The Story of Her Journey With Buddy Matthews
Gaming Updates LCFModGeeks
Gaming Updates LCFModGeeks: Stay Ahead With Modded Software and Gamer Content
Netflix Saves Sesame Street After Trump Cuts PBS Funds
Sesame Street Finds New Home on Netflix After PBS Funding Cut
michael j fox net worth
Michael J. Fox's Net Worth: A Closer Look at His Earnings and Assets

GAMING

Gaming Updates LCFModGeeks
Gaming Updates LCFModGeeks: Stay Ahead With Modded Software and Gamer Content
Gaming Communities
2025 Gaming Communities: Powering Creativity, Commerce, and Connection
Gaming Options Beyond Traditional Video Games
4 Types of Gaming Options That Go Beyond Traditional Video Games
Apple Blocks Fortnite on iOS
Fortnite Blocked on iOS in 2025 as Epic-Apple War Escalates
Best Mobile Games for Commuting Without Internet
Best Mobile Games for Commuting Without Internet

BUSINESS

Zach Bryan Crypto
Zach Bryan Crypto: Exploring The Crypto.com Arena in Los Angeles With Zach Bryan on Instagram
regeneron buys 23andme biotech acquisition
Regeneron Acquires 23andMe for $256M Amid Bankruptcy Woes
BNB vs Bitcoin
BNB: What makes it different from Bitcoin? 
3D Animation Company
When to Choose a 3D Animation Company Over 2D
Capital One Completes $35B Discover Deal
Capital One Completes $35B Discover Deal, Becomes Card Giant

TECHNOLOGY

Microsoft to Host Elon Musk’s Grok AI
Microsoft to Host Elon Musk’s Grok AI on Its Cloud Platform
Xiaomi chip investment
Xiaomi to Invest $7B in Chips to Boost Tech Independence
automotive industry trends
6 Trends Changing the Automotive Industry Forever
3D Animation Company
When to Choose a 3D Animation Company Over 2D
API Integration Mistakes and How to Avoid Them
5 Common API Integration Mistakes and How to Avoid Them

HEALTH

Joe Biden Faces Aggressive Prostate Cancer
Joe Biden Faces Aggressive Prostate Cancer, Family Reviewing Care
Stroke Patient May Be Nearing the End of Life
Recognizing When a Stroke Patient May Be Nearing the End of Life
PSA Test
For Men: Is the PSA Test Still Necessary?
Cattle Joint Supplements
Top Cattle Joint Supplements: Boosting Your Herd’s Health and Performance
Molar Implant Healing Timeline
The Healing Timeline After Getting Molar Implants