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

Harmanpreet Kaur Hails Shafali Verma Consistent Performer
Harmanpreet Kaur Lauds Shafali Verma: "She is someone who can always"
nicholas galitzine hollywood boundaries toxicity
Nicholas Galitzine Discusses Hollywood Boundaries & Avoiding Toxicity
damian lillard returns bucks game 6
Bucks Update: Damian Lillard Back for Game 6, Giannis Sidelined
filmy4cab
Filmy4Cab Features, Pros and Cons, Insights, and Top 100 Alternatives
anthropic launches claude ai for iphone
Anthropic Rolls Out Claude AI for iPhone, Challenges ChatGPT

LIFESTYLE

Anne hathaway sobriety journey
Anne Hathaway Celebrates Five Years of Sobriety
Most Expensive Handbags for Women in the World
Elegance Redefined: 10 Most Expensive Handbags for Women in the World
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

Entertainment

nicholas galitzine hollywood boundaries toxicity
Nicholas Galitzine Discusses Hollywood Boundaries & Avoiding Toxicity
filmy4cab
Filmy4Cab Features, Pros and Cons, Insights, and Top 100 Alternatives
Heeramandi review
Is "Heeramandi" a Masterpiece? Online Reviews Set the Web Ablaze
Emma Stone Nathan Fielder A24 Chess Scandal Movie
Emma Stone, Nathan Fielder Reunite for A24's Chess Scandal Film
What To Watch On Hulu In May 2024
New Movies and TV Shows Schedule of Hulu in May 2024 [Complete Guide]

GAMING

protect child from excessive gaming
How Can You Protect Your Child from Gaming Too Much?
wpc16
Top 20 Alternatives to WPC16 with WPC16 Dashboard Login [Image Guide]
20 Best Alternatives to WPC18 with Its Dashboard Login in 2024
20 Best Alternatives to WPC18 with Its Dashboard Login [Image Guide]
F95zone
How to Get Started on F95zone and Increase Community Interaction in 2024 [Gamer's Guide]
Ghost of Tsushima pc
Ghost of Tsushima PC Release Date, Features, Requirements, and More

BUSINESS

How to Select Lab Equipment Service Providers
How to Evaluate and Select Lab Equipment Service Providers
Tech giant huawei profit growth and apple sales
Huawei Profits Soar 564%, Cutting Deeply into Apple's Market Share
Elon Musk in China as rivals show new electric vehicles
Elon Musk in China: Tesla Faces Rising EV Competition
covid 19 impact on cleaning industry
The Impact of COVID-19 on the Cleaning Industry and How Businesses are Adapting
Japanese Yen Dollar Exchange Rate Surge
Japanese Yen Rebounds Strongly From 1990 Low Against the Dollar

TECHNOLOGY

anthropic launches claude ai for iphone
Anthropic Rolls Out Claude AI for iPhone, Challenges ChatGPT
twitch tv activate on xbox
Twitch TV Activate on Xbox, PlayStation, and Android [Complete Image Guide]
smartphone market q1 xiaomi samsung apple ranking
Xiaomi Holds Global #3 Spot in Q1, Trails Samsung and Apple
Mark Zuckerberg Announces whatsapp community upgrades
Mark Zuckerberg Announces New Features for WhatsApp Communities
Microsoft Expands AI cloud in Indonesia
Microsoft CEO Commits $1.7B for AI and Cloud in Indonesia

HEALTH

6 Ways Mindfulness Can Alleviate Your Pain
6 Ways Mindfulness Can Alleviate Your Pain
King Charles Resumes Duties After Health Update
King Charles Resumes Duties After Health Update on Cancer Battle
What to Do When Testosterone Levels Drop
What to Do When Testosterone Levels Drop Too Low and How to Treat
Can Tonsils Grow Back After Being Removed? - Tymoff
Can Tonsils Grow Back After Being Removed? - Tymoff
impact of emotional trauma on chronic pain
Who is Most Affected by Emotional Trauma-Induced Chronic Pain?