Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

laravel
http
communitycreator

What are Laravel basic routing and available router methods?

Chinweuba Elijah Azubuike

What are router methods in Laravel?

Router methods enable the application to respond to any HTTPHyperText Transfer Protocol request. In cases where you have to respond to an HTTP request method like the PUT PATCH DELETE.

Router

The router method comes in handy. HTTP methods such as POST and GET are the most familiar once we know for basic form submission. We see methods such as PUT when we deal with API.

Syntax


Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

The any() method

When we build applications, sometimes we want to respond or catch HTTP requests sent to that route.

Laravel provides a fluent way to respond to any HTTP request using the any() method like this:


Route::any('/', function () {
    //
});

The method above will respond to any HTTP request directed to that uri.

We might want to filter the HTTP method that we wish to respond to, so we use the match() method.


Route::match(['get', 'post'], '/', function () {
    //
});

Thus, we specify the HTTP method we want to respond to.

Summary


The route method is used to catch specific HTTP requests to our application where we don’t want to limit our application to just the GET and POST methods.

RELATED TAGS

laravel
http
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring