Laravel 8 Tutorial - Routing

In this video, we will learn about routing in Laravel, an essential concept that allows us to route all our application requests to their appropriate controllers.

All Laravel routes are defined in route files, which are located in the routes directory. These files are automatically loaded by the framework. Let's take a look at the route files. In the project root directory, you'll see the routes folder, which contains web.php and api.php files.

The web.php file defines routes for the web interface, which are assigned the web middleware group, providing features like session state and CSRF protection. The routes defined in web.php can be accessed by entering the defined route's URL in your browser.

The routes in api.php are stateless and are assigned the api middleware group. Now, let's create a route. Open the api.php file and add the following code:


Route::get('/user', function (){
return "Hi user";
});

This route has a URI of /user and a callback method that returns "Hi User". Let's test this API route using Postman.

In Postman, enter the URL localhost:8000/api/user, and you should see the response "Hi User". We can also pass a parameter with the route. Laravel provides two ways of capturing the passed parameter: required and optional parameters.

Let's see how to use required parameters. Modify the route to accept a parameter:


Route::get('/user/{$name}', function ($name){
return "Hi ".$name;
});

In Laravel, the dot (.) is used for concatenation. Now, test this route by passing a parameter in Postman.

Next, let's see how to use optional parameters:


Route::get('/user/{$name?}', function ($name=null){
return "Hi ".$name;
});

Now, the parameter has become optional. Test this route in Postman, and you'll see that it still works even when the parameter is not provided.

Now, let's see how to constrain the routes:


Route::get('/user/{$name}', function ($name){
return "Hi ".$name;
})->where('name','[a-zA-Z]+');

This route only accepts alphabetic characters, not numeric values. Test this route in Postman, and you'll see that it throws an error when a numeric value is entered.

Let's create another route that only accepts numeric values:


Route::get('/user-ids/{id}', function ($id){
return "Hi, Your id is ". $id;
})->where('id','[0-9]+');

Test this route in Postman, and you'll see that it only works with numeric values.

Now, let's see how to add global constraints:


Route::pattern('id', '[0-9]+');

Route::pattern('name', '[a-zA-Z]+');

These constraints are applied to each route. Test this route in Postman, and you'll see that it only works with alphabetic characters.

Sometimes, we need to register a route that responds to multiple HTTP verbs. We can do this using the match method:


Route::match(['get', 'post'], '/', function (Request $req)
{
return "hi there, the requested method" . $req->method();
});

Test this route in Postman, and you'll see that it responds to both GET and POST requests.

We can also register a route that responds to all HTTP verbs using the any method:


Route::any('/post', function (Request $req) {
retun "Requested method is ". $req->method();
});

Test this route in Postman, and you'll see that it responds to all HTTP verbs.

That's all about Laravel 8 routing!