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

india neutralizes pakistani drone attacks
India Neutralizes Pakistani Drone Strikes Amid Rising Clashes
Jules Ari
Jules Ari Age, Height, Relationship, Family, Biography, and Net Worth
Robert Prevost Becomes Pope Leo XIV
Robert Prevost Becomes Pope Leo XIV: First American Pontiff
Historical Events and Famous People Born on May 9
Discover the Historical Events and Famous People Born on May 9
May 9 Zodiac
What's in Your Zodiac If You Born on May 9 [Life, Career, Relationship]

LIFESTYLE

summer birthday party ideas
Creative Summer Birthday Party Ideas for Kids in 2025
May 6 Zodiac
May 6 Zodiac: Positive Traits, Compatibility and More about Taurus
self storage solutions for life transitions
How Self Storage Can Help During Major Life Changes (Divorce, Moving, etc.)?
why is my poinsettia dying
Why Is My Poinsettia Dying? Tips To Revive Your Wilting Poinsettia Plant
crypto retirement plan strategies
7 Ways Crypto Can Reshape Your Retirement Plan for the Future

Entertainment

Jules Ari
Jules Ari Age, Height, Relationship, Family, Biography, and Net Worth
rocket league unblocked
Rocket League Unblocked: Soccer And Vehicular Mayhem Online Game
smokey robinson sexual assault allegations
Smokey Robinson Faces Sexual Assault Allegations by 4 Women
Disneyland Abu Dhabi
Disneyland Abu Dhabi: First Disney Theme Park in Middle East
Netflix OpenAI Partnership
Netflix Teams Up with OpenAI to Transform Streaming Experience

GAMING

Best Mobile Horror Games
The Best Mobile Horror Games That Will Keep You Up at Night
Evolution of Video Game Graphics
The Evolution of Video Game Graphics: 1980s to 2025
Best Workouts Inspired by Video Games
Level Up Your Fitness: Best Video Game-Inspired Workouts
rocket league unblocked
Rocket League Unblocked: Soccer And Vehicular Mayhem Online Game
Maksym Krippa GSC Game World
S.T.A.L.K.E.R. Reimagined: How Maksym Krippa’s Entry Reshaped GSC Game World in 2023

BUSINESS

Business Behind Game Localization
The Business Behind Game Localization: How It Works
International Employment Agencies
How international employment agencies can help you find talent in hard-to-reach markets?
Transition Your Business to Web3
How to Transition Your Business to Web3 Successfully
How to Calculate Quarterly Tax Payments
How to Calculate Quarterly Tax Payments in 5 Easy Steps
credit suisse tax evasion
Credit Suisse Fined $511M for U.S. Offshore Tax Evasion Scheme

TECHNOLOGY

Maksym Krippa GSC Game World
S.T.A.L.K.E.R. Reimagined: How Maksym Krippa’s Entry Reshaped GSC Game World in 2023
Strengthening Cybersecurity with Security Operations
Strengthening Cybersecurity with Security Operations, CWPP, and Product Security
pitch a game idea
How to Pitch a Game Idea to a Developer or Publisher?
Web3 impact on virtual reality experiences
How Web3 Is Redefining Virtual Reality and Immersive Experiences?
Smart Contracts in Web3
How Smart Contracts Are Shaping the Future of Web3?

HEALTH

Yimusanfendi
7 Incredible Benefits of Yimusanfendi Meditation and Possible Side Effects
Connection Between Hydration and Urinary Health
The Connection Between Hydration and Urinary Health
Neuralink Brain Implant Patient Regains Speech
Neuralink Brain Implant Helps ALS Patient Regain Speech with AI Support
Wegovy for Weight Loss
Wegovy for Weight Loss: Is It Worth Buying Online?
Role of Sperm DNA Fragmentation Testing in IVF
The Role of Sperm DNA Fragmentation Testing in IVF with ICSI Success