DEV Community

Cover image for Explain the concept of Laravel Routing
Brijesh Dobariya for CodedThemes

Posted on • Updated on

Explain the concept of Laravel Routing

Routing is one of the essential concepts in Laravel. Routing in Laravel allows you to route all your application requests to its appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing. In this article we will discuss a brief concept of Laravel routing.

What is Laravel routing?

Using Routing you can create a request URL for your application. you can design set of HTTP request like POST Request, GET Request, PUT Request and DELETE Request using routing in Laravel 8.

In the Laravel routes folder contain two routes’ files, one is api.php, which is used to API routes and another is web.php, which is used for routes for the web application.

Routing in Laravel includes various methods. We explore each one with an example code step by step.

• Basic Routing

• Route Parameters

• Named Routes

• Route Groups

• Route Model Binding

Basic routing

Most of the routes for your application will be defined in the app/routes.php file. The simplest Laravel routes consist of a URI and a Closure callback.

Basic GET Route

Route::get('/', function()
{
    return 'Hello World';
});
Enter fullscreen mode Exit fullscreen mode

Basic POST Route

Route::post('foo/bar', function()
{
    return 'Hello World';
});
Enter fullscreen mode Exit fullscreen mode

Registering A Route For Multiple Verbs

Route::match(array('GET', 'POST'), '/', function()
{
    return 'Hello World';
});
Enter fullscreen mode Exit fullscreen mode

Registering A Route Responding To Any HTTP Verb

Route::any('foo', function()
{
    return 'Hello World';
});
Enter fullscreen mode Exit fullscreen mode

Forcing A Route To Be Served Over HTTPS

Route::get('foo', array('https', function()
{
    return 'Must be over HTTPS';
}));
Enter fullscreen mode Exit fullscreen mode

Often, you will need to generate URLs to your routes, you may do so using the URL::to method:

$url = URL::to('foo');
Enter fullscreen mode Exit fullscreen mode

Route parameters

When we need to capture the parameter passed to the URL, route parameters work for it. We can achieve this in two ways.

Required Parameter:

Using this method its compulsory to capture the passed parameter. For example, getting the user ID from the URL.

Route::get('user/{id}', function ($id) 
{
    return 'User Id is:'.$id;
});
Enter fullscreen mode Exit fullscreen mode

Optional parameter

Sometimes we need to make route parameters as optional, then we use this method. Just place the ‘?’ symbol after the parameter name. Make sure that you have a pass the default value to that variable.

Route::get('customer/{name?}', function ($name = null) {
    return $name;
});
Enter fullscreen mode Exit fullscreen mode

Named routes

In Laravel we can set the name of the route. This feature requires some time, for example, you want to redirect a user one to another route than no need to define complete redirect URL, just give its name.

If you have using the names routes method and want to change the URL structure then only change the web.php file. See the example code below:

Route::get('/task/{id}', function($id)
{
    return route('task',['id' => 2]);
})->where('id', '[0-9]+')->name('task');
Enter fullscreen mode Exit fullscreen mode

Explanation: Here, task is the route name and ‘route(‘task’,[‘id’ => 2])’ is the output value.

Route groups

you can combine large number of routes in group to share route attributes like namespaces or middleware. No need to define an attribute for each route.

In the below example, we combine the ‘auth’ middleware with URL prefix ‘admin’.

Route::group(['prefix' => 'admin',  'middleware' => 'auth'], function()
{
    Route::get('dashboard', function() {} );
    Route::get('settings', function() {} );
});
Enter fullscreen mode Exit fullscreen mode

Route Model Binding

This method you can inject a model ID into your route or controller action. This method has two ways one in Implicit binding and another one is Explicit Binding.

Route::get('posts/{post}', function ($post) {
    // return the view and the post
    return view('post.show', compact('post'));
});
Enter fullscreen mode Exit fullscreen mode

Explanation: $post we can inject the post object. Inject the ‘post’ model into a route controller.

Conclusion

Routing is one of the important concepts of web applications. It allows you to quickly manage various URLs' structures. The Laravel routing feature makes the process very easy.

For more Article: Click here

Latest comments (0)