Routing in Laravel

In Laravel, you can define routes using the Route facade, which provides a simple and expressive way to define HTTP routes. Following are some examples of routes how to create routes in Laravel.

Route is a URL pattern that maps to a specific controller action or function in your application. Laravel provides a powerful routing system that allows you to define routes for different HTTP methods like GET, POST, PUT, DELETE, etc.

Laravel has two types of route files one is web.php and the second is api.php. web.php defined the web or HTTP route and api.php defined the API calling route. In the API route automatically add api prefix in the URL. api prefix will add after base URL.

Step 1: Basic Route

The most basic route definition in Laravel looks like this.

Route::get('/', function () {
    return view('welcome');
});

This creates a route that responds to HTTP GET requests to the root URL (/) and returns the welcome view.

Step 2: Route with Parameters

You can define routes that accept parameters by enclosing the parameter name in curly braces {}.

Route::get('/users/{id}', function ($id) {
    return "User ID: " . $id;
});

This creates a route that accepts a parameter called id and returns a string with the value of the parameter.

Step 3: Route with Optional Parameters

You can make a route parameter optional by enclosing it in curly braces with a question mark ?.

Route::get('/products/{id?}', function ($id = null) {
    if ($id) {
        return "Product ID: " . $id;
    } else {
        return "All Products";
    }
});

This creates a route that accepts an optional parameter called id, and if provided, returns the product ID. If the parameter is not provided, it returns a message indicating all products.

Step 4: Named Route

You can give a name to a route by chaining the name method to the route definition.

Route::get('/contact', function () {
    return view('contact');
})->name('contact');

This creates a route that responds to HTTP GET requests to /contact and gives it a name contact. You can use the name later in your application to generate URLs or redirects.

Step 5: Route Group

You can group multiple routes that share the same middleware or prefix.

Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', function () {
        return view('dashboard');
    });
    Route::get('/profile', function () {
        return view('profile');
    });
});

This creates a group of routes that use the auth middleware, which means that the user must be authenticated to access them.

You can also define a route that maps to a controller method instead of a closure function. Here is an example of defining a route that maps to a UserController method.

Route::get('/users', 'UserController@index');

In the example above, we define a route that responds to the /users URL and maps to the index method of the UserController.

 

Hope this blog is helpful to you. If you have any queries or feedback please comment to me.

Leave a Reply

Your email address will not be published. Required fields are marked *