CLOSE

In Laravel, routing refers to the process of defining endpoints (or routes) that map incoming HTTP requests to the appropriate controller actions or closures. Lara

Basic Route Definition

The most basic route accepts a URI and a closure, defining the behavior for that route. For example:

use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
    return 'Hello World';
});

Available Router Methods

Laravel's router supports various HTTP verbs for route registration, including GET, POST, PUT, PATCH, DELETE, and OPTIONS. You can register routes for different HTTP verbs using methods like get, post, etc. Additionally, there are match and any methods for handling multiple verbs or all verbs respectively.

1. GET:

  • The get method is used to register a route that will respond to HTTP GET requests.
  • Used for retrieving data form the server.
  • Commonly used for fetching web pages, images, scripts, etc.
  • Data is sent via query parameters in the URL.
Route::get('/profile/{user}', 'ProfileController@show');

When a user navigates to /profile/{user} where {user}is a dynamic parameter representing the user's ID or username, Laravel will invoke the show method of the ProfileController. This method is responsible for displaying the profile page of the specified user.

2. POST:

  • Used for submitting data to the server.
  • Commonly used for form submissions, file uploads, etc.
  • Data is sent in the body of the HTTP request.
  • The data sent can be in various formats such as JSON, XML, etc.
  • For example, submitting a login form, creating a new record in a database.
Route::post('/posts', 'PostController@store');

When a form is submitted to /posts, Laravel will trigger the store method of the PostController. This method is responsible for processing the submitted form data and storing it, typically used for creating a new post.

3. PUT/PATCH:

  • Used for updating existing resources on the server.
  • PUT is traditionally used to update the entire resource, while PATCH is used to update a part of the resource.
  • Data is sent in the body of the HTTP request.
  • Typically used in RESTful APIs for updating resources.
Route::put('/profile/{user}', 'ProfileController@update');

Route::patch('/profile/{user}', 'ProfileController@update');

When a PUT or PATCH request is made to the /profile/{user}, Laravel will call the update method of the ProfileController. This method updates the profile information of the specified user.

4. DELETE

  • Used for deleting resources on the server.
  • Sends a request to remove the specified resource.
  • Data can be sent in the body, but it's not commonly done; instead, the resource to be deleted is specified in the URL.
Route::delete('/posts/{post}', 'PostController@destroy');

When a DELETE request is sent to /posts/{post}, Laravel will invoke the destroy method of the PostController. This method deletes the post identified by the {post} parameter.

5. match

The match method in Laravel allows you to define a route that responds to multiple HTTP request methods (verbs) like GET, POST, PUT, PATCH, DELETE, etc. It's convenient way to define a single route that handles multiple types of requests.

Here's how you can use the match method:

Route::match(['get', 'post'], '/example', 'ExampleController@action');

Here, match method is used to define a route. The first argument is an array of HTTP request methods that the route should respond to (in this case, both GET and POST). The second argument is the URI pattern (/example). The third argument is the controller action that should be executed when the route is accessed.

This route will match both GET and POST requests to /example, and it will invoke the action method of the ExampleController.

6. any:

The any method in Laravel allows you to define a route that responds to any HTTP request method (GET, POST, PUT, PATCH, DELETE, etc.). This method is useful when you want a single route to handle all types of requests for a particular URI pattern.

Here's how you can use the any method in Laravel:

Route::any('/example', 'ExampleController@action');

In this example:

  • any method is used to define a route.
  • The first argument is the URI pattern (/example).
  • The second argument is the controller action that should be executed when the route is accessed.

This route will match any type of HTTP request (GET, POST, PUT, PATCH, DELETE, etc.) to /example, and it will invoke the action method of the ExampleController.

Dependency Injection

Laravel's service container allows for automatic injection of dependencies into route callbacks. This allows you to type-hint dependencies in route callbacks. For example, injecting the current HTTP request:

use Illuminate\Http\Request;

Route::get('/users', function (Request $request) {
    // ...
});

 

At its core, defining routes in Laravel is straightforward yet flexible. Using the Route:: facade, developers can map URLs to controller actions with ease. Let's take a closer look at a basic route definition:

Route::get('/about', 'AboutController@index');

In this example, we are defining a route that responds to a GET request made to the /about URL, directing it to the index method of the AboutController. This simple syntax encapsulates the essence of routing in Laravel, allowing developers to create intuitive and expressive routes effortlessly.